InputSystem을 이용해 WASD값 생성
Player.cs
public PlayerInput Input;
private Rigidbody Rigidbody ;
flaot speed = 5f
private void Awake //초기화
{
Input = GetComponent<PlayerInput>();
Rigidbody = GetComponent<Rigidbody>();
}
public Vector2 MovementInputInputSystem; //생성한 이름
PlayerInput.cs
public PlayerInputActions InputActions { get; private set; }
public PlayerInputActions.PlayerActions PlayerActions { get; private set; }
여기서 PlayerActions는 액션 맵 구조체 유형의 이름
Player는 해당 구조체 유형의 변수 이름이다.
private void Awake()
{
InputActions = new PlayerInputActions();
PlayerActions = InputActions.Player;
}
초기화 해주고
아래에 것도 까먹지말고 바로 해주기
private void OnEnable()
{
InputActions.Enable();
}
private void OnDisable()
{
InputActions.Disable();
}
player.cs
public void PhysicsUpdate() //물리관련이므로 Physcis업데이트
{
Move();
}
private void ReadMovementInput()
{
MovementInput = Input.PlayerActions.Movement.ReadValue<Vector2>();
}
//인풋시스템은 정규화 작업이 필요없다. PlayerInput <Vector2>
//예를들어 W와 D를 눌러 대각선 오른쪽으로 이동하면 (0.7, 0.7)이 됨.
private void Move()
{
if (MovementInput == Vector2.zero || speed == 0f)
{
return; //움직임 없거나 속도0이면 Move안함.
}
Vector3 movementDirection = new Vector3(MovementInput.x, 0f, MovementInput.y);
Rigidbody.AddFroce(movementDirection * speed, ForceMode.VelocityChange)
}
에서 끝내지 않고, AddForce()에서 수평속도를 제거하자.
왜냐하면 AddForce에는 이미 존재하는 힘에 속도를 추가해버리기 때문에 기존 속도를 제거해야만한다.
Vector3 playerHorizontalVelocity = Rigidobdy.velocity;
playerHorizontalVelocity.y = 0f;
Rigidbody.AddFroce(movementDirection * speed - "playerHorizontalVelocity" , ForceMode.VelocityChange)
Rigidbody로 움직임을 줄때는 Time.deltatime을 사용하지 않을 것.
또한 Rigidobdy로 이동을 처리할때 수평 속도를 제거하자.
플레이어가 새로운 방향으로 즉시 전환해야할때 수평속도를 제거해야 새 방향으로 속도 적용이 가능하다.
갑작스러운 방향 변경에 유용.
즉, 수평 속도 제거는 요구 사항 및 이동 감각이 중요할때 물리 법칙을 현실적으로 모사해
플레이어에게 더 직관적이고 통제가능한 이동경험을 줌.