유니티

유니티 입문 - 2 리지드 바디

당혜성 2025. 5. 1. 19:46

rigidbody = 물리
use Gravity = 중력
mass = 질량
drag = 공기 저항 숫자가 높을수록 천천히 떨어짐
is kinematic = 물리 해제, 중력, 충돌, 관성등 다 적용x
interpolate = 캐릭터의 움직임을 보관해 자연스럽게 만들어줌
Extrapolate = 다음 움직임 예측
Collision Detection = 충돌 탐지
Continuous, Continuous Dynamic = 총알같이 빠른애한테 적용하면 좋음
Constraints = x y z 축을 얼린다. 위치값 고정

리지드바디를 유니티에서 적용하는 방법

코드 예시

  private Rigidbody myRigid;

  private void Start()
  {
      myRigid = GetComponent<Rigidbody>();   
  }


velocity = 속도
예시 코드

myRigid.velocity = new Vector3(0, 0, 1);


회전 속도 주기

myRigid.angularVelocity = new Vector3(1,0,0);


회전 속도 최대

myRigid.maxAngularVelocity = 100;


엄청난 속도로 회전

if (Input.GetKey(KeyCode.W))
{
    myRigid.maxAngularVelocity = 100;
    myRigid.angularVelocity = Vector3.right * 100;
}


myRigid.useGravity = true, false 중력 껏다 키기

일정한 방향으로 이동

myRigid.MovePosition(transform.forward);



w를 누를때마다 회전

private Vector3 rotation;
 private void Start()
 {
     myRigid = GetComponent<Rigidbody>();
     rotation = this.transform.eulerAngles;
 }
 private void Update(){
	if (Input.GetKey(KeyCode.W))
	{
    	rotation += new Vector3(90, 0, 0) * Time.deltaTime;
    	myRigid.MoveRotation(Quaternion.Euler(rotation));
	}
}



관성과 질량의 영향을 받는거

myRigid.AddForce(Vector3.forward);



꾹 누르면 계속 회전하다가 때도 회전력이 남아있음. 점점 느려지다가 멈춤
관성

myRigid.AddTorque(Vector3.up);



폭발을 할때 필요한거

myRigid.AddExplosionForce(10, this.transform.right, 10);


폭발력, 폭발 위치, 폭발 반경 순

add 시리즈는 전부 다 물리 효과와 관련이 있다.
move는 강제적으로 이동시키는거라 물리 효과가 없다