◼ IT Etc./Unity

[유니티 기초] #2. 리지드 바디 (RigidBody) 기능 정리

SangYoonLee (SYL) 2021. 3. 22. 01:25
반응형

중력 설정

 

 - Use Gravity 설정 체크시 적용

 

 

- 중력 속도 설정 : [Edit] -> [Project Settings...] -> [Gravity]의 x, y, z축 각각에 해당하는 숫자가 각 축에 적용된 중력 속도

 

스크린샷 사진이 잘 안보이시면 사진을 클릭하세요 

 

 

 

 

리지드 바디(Rigidbody)의 각 항목 기능

 

                 - Mass : 질량

 

                 - Drag : 공기 저항 (0은 우주 공간)

 

                               중력 적용시 Drag 수치가 높을수록 물체가 천천히 떨어짐

 

                 - Angular Drag : 회전값에 대한 저항

 

                 - Use Gravity : 물체에 대한 중력 적용 여부

 

                 - Is Kinematic : 물리 효과 소멸 적용 여부 (체크 시 물리 효과 X)

 

                 - Interpolate : 캐릭터의 움직임이 부자연스러울 떄 자연스럽게 처리하도록 돕는 도구

                   └ Interpolate : 이전 프레임의 움직임을 통해 다음 프레임을 만들어냄

                   └ Extrapolate : 다음 프레임의 움직임을 예측

 

                 - Collision Detection : 충돌 탐지기

                   └ Discrete : 기본 값

                   └ Continuous, Continuous Dynamic : 보다 정밀한 물리 효과

                                                                       (총알 같이 빠른 물체에게 적용하면 좋음)

 

                 - Constraints : Position(위치) 값, Rotation(회전) 값을 고정시키는 기능 (체크 시 값 변환 X)

 

 

 

 

 

 

스크립트로 Rigidbody 컴포넌트 제어하기

(1) velocity, angularVelocity 이용해 속도, 회전 속도 조절

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Test : MonoBehaviour
{
    [SerializeField]
    private Rigidbody myRigid;

    void Start ()
    {
        myRigid = GetComponent<Rigidbody>();
        // myRigid로 오브젝트의 Rigidbody 컴포넌트 제어 가능
    }

    void Update()
    {
        if(Input.GetKey(KeyCode.W))
        {
            // velocity : 속도 조절
            //myRigid.position //transform.position과 비슷한 역할
            myRigid.velocity = Vector3.forward; // == Vector3(0, 0, 1)
            
            // angularVelocity : 회전 속도 조절
            // 왼쪽으로 회전
            myRigid.angularVelocity = -Vector3.right; //new Vector3(1, 0, 0);
        }
    }
}

 

 

 

(2) 기타 속성 값 변경

 void Update()
    {
        if(Input.GetKey(KeyCode.W))
        {
            myRigid.mass = 21;  // 질량 변경
            myRigid.drag = 21;  // 저항 변경
            myRigid.angularDrag = 1;   // 회전 저항 변경 
            
            // 회전 최대 속도 변경 (기본값: 7)
            myRigid.maxAngularVelocity = 100;
            // 변경한 후, 오른쪽으로 회전
            myRigid.angularVelocity = Vector3.right * 100;
            
            // 체크/헤제 속성 변경
            myRigid.isKinematic = true;
            mtRigid.useGravity = true;
        }

 

 

 

(3) 대표적인 메소드 - 1. MovePosition

 

(괄호)내 위치로 이동

    void Update()
    {
		if(Input.GetKey(KeyCode.W))
        {
            // (괄호)내 위치로 이동
            myRigid.MovePosition(transform.forward);
            // 1초씩 이동
            myRigid.MovePosition(transform.forward * Time.deltatime);
        }
    }

 

 

 

(4) 대표적인 메소드 - 2. MoveRotation

 

관성과 질량을 무시하고 이동. Quaternion 값 대입해줘야 함

(W키를 떼면 바로 멈춤. 관성 작용 X)

public class Test : MonoBehaviour
{
    [SerializeField]
    private Rigidbody myRigid;
    
    private Vector3 rotation;

    void Start ()
    {
        myRigid = GetComponent<Rigidbody>();
        // myRigid로 오브젝트의 Rigidbody 컴포넌트 제어 가능

        rotation = this.transform.eulerAngles;
    }

    void Update()
    {
        if(Input.GetKey(KeyCode.W))
        {
            rotation += new Vector3(90, 0, 0) * Time.deltaTime;
            myRigid.MoveRotation(Quaternion.Euler(rotation));
        }
    }
}

 

 

 

(5) 대표적인 메소드 - 3. AddForce

 

특정 방향으로 힘을 가함

(W키를 떼면 힘 준 방향으로 천천히 멈춤. 관성 작용)

    void Update()
    {
        if(Input.GetKey(KeyCode.W))
        {
            myRigid.AddForce(Vector3.forward);
        }
    }

 

 

 

(5) 대표적인 메소드 - 4. AddTorque

 

특정 방향으로 회전 + 관성 작용

    void Update()
    {
        if(Input.GetKey(KeyCode.W))
        {
            myRigid.AddTorque(Vector3.up);
        }
    }

 

 

 

 

(6) 대표적인 메소드 - 5. AddExplosionForce

 

폭발 물리 효과 (세기. 위치, 반경)

    void Update()
    {
        if(Input.GetKey(KeyCode.W))
        {
            // 오른쪽에서 폭발 -> 왼쪽 방향으로
            myRigid.AddExplosionForce(10, this.transform.right, 10);
        }
    }

 

 

 

(7) 메소드 이름과 물리효과 관계

 

Add~ : 물리 효과

Move~ : 강제 이동 (물리 효과X)

 

 

 

 

 

이 글은 케이티 님의 '유니티 입문 강좌 part 3 - 컬라이더' 강의를 바탕으로 작성한 글입니다.

 

 

반응형