Files
SniperGame/Assets/PlayerMove.cs
2026-02-17 14:51:52 +09:00

110 lines
3.0 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MyGame;
using UnityEngine.InputSystem;
using Unity.VisualScripting;
public class PlayerMove : MonoBehaviour
{
private GameInput gameInput;
private PlayerInput playerInput;
private Rigidbody rb;
private float currentMoveSpeed = 5;
public float moveSpeed;
public float sprintSpeed;
public float rotateSpeed;
private Camera CinemachineCamera;
private Vector2 moveInputValue;
private bool attack;
public Animator animator;
void Awake()
{
rb = GetComponent<Rigidbody>();
CinemachineCamera = Camera.main;
gameInput = new GameInput();
playerInput = GetComponent<PlayerInput>();
SetInputAction();
gameInput.Enable();
}
private void SetInputAction()
{
gameInput.Player.Move.started += OnMove;
gameInput.Player.Move.performed += OnMove;
gameInput.Player.Move.canceled += OnMove;
gameInput.Player.Attack.performed += context => attack = true;
gameInput.Player.Attack.canceled += context => attack = false;
}
void OnMove(InputAction.CallbackContext context)
{
moveInputValue = context.ReadValue<Vector2>();
}
void Update()
{
// アニメーション用の速度を計算
float animationSpeed = 0;
animationSpeed = moveInputValue.magnitude;
currentMoveSpeed = moveSpeed;
// アニメーターの "Speed" パラメータに値を渡す
animator.SetFloat("Speed", animationSpeed, 0.1f, Time.deltaTime);
}
private void FixedUpdate()
{
Vector3 horDirection = moveInputValue.x * CinemachineCamera.transform.right;
Vector3 verDirection = moveInputValue.y * CinemachineCamera.transform.forward;
horDirection.y = 0f;
verDirection.y = 0f;
Vector3 moveDirection = (horDirection + verDirection).normalized;
Vector3 rotateDirection = CinemachineCamera.transform.forward;
rotateDirection.y = 0f;
Quaternion targetRotation = Quaternion.LookRotation(rotateDirection);
rb.rotation = Quaternion.Lerp(rb.rotation, targetRotation, rotateSpeed * Time.fixedDeltaTime);
if (moveDirection.sqrMagnitude > 0.01f)
{
rb.linearVelocity = new Vector3(moveDirection.x * currentMoveSpeed,
rb.linearVelocity.y,
moveDirection.z * currentMoveSpeed
);
}
}
/*public void FootStep()
{
AudioManager.Instance.PlaySE("Run");
}
public void Death()
{
if (animator != null)
{
animator.SetBool("Death", true);
}
if (playerInput != null)
{
playerInput.enabled = false;
}
this.enabled = false;
if (rb != null)
{
rb.linearVelocity = Vector3.zero;
rb.angularVelocity = Vector3.zero;
rb.isKinematic = true;
}
}*/
}