스토리지

[4.14] Rigidbody를 사용해서 캐릭터 이동 및 회전 본문

Unity/유니티 기본

[4.14] Rigidbody를 사용해서 캐릭터 이동 및 회전

ljw4104 2021. 4. 14. 11:26

캐릭터를 이동시키는 법

  1. transform.Translate();
  2. playerRigidbody.AddForce()
  3. playerRigidbody.MovePosition

Rigidbody.MovePosition

  • 상대적으로 이동할 거리를 계산 후 지금 Position에 더해준다
  • 이동할 거리 : Vector3 movement = Inut.GetAxis("Vertical") * transform.forward * moveSpeed * Time.deltaTime;
  • Rigidbody.MovePosition(this.playerRigidbody.position + movement)

 

캐릭터를 회전시킨 법

  1. transform.Rotate()
  2. playerRigidbody.rotation

Rigidbody.rotation

  • rotation에는 Quaternion 값이 들어간다 => Euler로 변경 후 값을 변경해주어야함.
  • 먼저 상대적인 회전값을 계산 후 그 값을 Quaternion으로 변경 후 값을 넣어줌.
  • 회전값 : var turn = Input.GetAxis("Horizontal") * rotationSpeed * Time.deltaTime;
  • playerRigidbody.rotation = playerRigidbody.rotation * Quaternion.Euler(0,turn,0)
  • (Quaternion은 행렬곱으로 연산되기 때문에 연산이 곱셈밖에 없다)

Transform과의 차이점

  • Rigidbody를 사용해서 하는것은 Physics Engine을 사용해서 계산 후 처리한다.

'Unity > 유니티 기본' 카테고리의 다른 글

[4.14] Nav Mesh  (0) 2021.04.14
[4.14] Coroutine  (0) 2021.04.14
[4.9] 닷지 게임  (0) 2021.04.09
[4.9] Material & Shader  (0) 2021.04.09
[4.8] 유니티 PVP게임 샘플 1차  (0) 2021.04.08
Comments