RigidBody 란?
: GameObject가 물리 제어로 동작하게 만들어주는 역할을 하는 컴포넌트, 중력의 영향을 받으며 오브젝트에 가해지는 힘으로 움직이는 동작을 이야기함
리지드바디 - Unity 매뉴얼
Rigidbody 는 GameObject 가 물리 제어로 동작하게 합니다. 리지드바디는 힘과 토크를 받아 오브젝트가 사실적으로 움직이도록 해줍니다. 리지드바디가 포함된 모든 게임 오브젝트는 중력의 영향을
docs.unity3d.com
1. RigidBody의 AddForce(Vector3 force, ForceMode mode) 이용
🔸force : 힘의 방향과 크기를 지정
🔸mode : 힘을 주는 모드
Unity - Scripting API: Rigidbody.AddForce
Force is applied continuously along the direction of the force vector. Specifying the ForceMode mode allows the type of force to be changed to an Acceleration, Impulse or Velocity Change. The effects of the forces applied with this function are accumulated
docs.unity3d.com
if (Input.GetKey(KeyCode.UpArrow) == true)
{
rigid.AddForce(0f, 0f, speed);
}
if (Input.GetKey(KeyCode.DownArrow) == true)
{
rigid.AddForce(0f, 0f, -speed);
}
if (Input.GetKey(KeyCode.RightArrow) == true)
{
rigid.AddForce(speed, 0f, 0f);
}
if (Input.GetKey(KeyCode.LeftArrow) == true)
{
rigid.AddForce(-speed, 0f, 0f);
}
2. RigidBody의 velocity 이용
🔸Vector velocity
Unity - Scripting API: Rigidbody.velocity
In most cases you should not modify the velocity directly, as this can result in unrealistic behaviour - use AddForce instead Do not set the velocity of an object every physics step, this will lead to unrealistic physics simulation. A typical usage is wher
docs.unity3d.com
private void Update()
{
float xInput = Input.GetAxis("Horizontal");
float yInput = Input.GetAxis("Vertical");
float xSpeed = xInput * speed;
float ySpeed = yInput * speed;
Vector3 newVelocity = new Vector3(xSpeed, 0f, ySpeed);
rigid.velocity = newVelocity;
}
RigidBody의 AddForce와 velocity의 차이
: 관성의 차이
🔸AddFoce : 힘을 누적하고 속력을 점진적으로 증가
- 순간적으로 튀어 오르고 점차 속도가 줄어들면서 떨어지는 점프에 적합
🔸velocity : 이전 속도를 지우고 새로운 속도를 사용, 관성을 무시하고 속도를 즉시 반영
- 동일한 속도를 달리는 러너게임 캐릭터에 적합
'Unity' 카테고리의 다른 글
[ 내일배움캠프 ] TIL.20 - 애니메이션 블렌드 ( 4방향 애니메이션 ) (0) | 2024.05.13 |
---|---|
[ 내일배움캠프 ] TIL.19 - 스크립터블오브젝트(Scriptable Object)? (0) | 2024.05.10 |
[ Unity ] 벡터(Vector) (0) | 2024.04.27 |
[ Unity ] Unity Ads 광고 추가하기 (0) | 2024.03.27 |
[ Unity ] Unity Recorder을 이용하여 영상 녹화하기 (0) | 2024.03.20 |