記錄

Roll a ball -1 본문

Game/Unity

Roll a ball -1

surhommejk 2018. 1. 8. 13:35

Rigidbody


Description

Control of an object's position through physics simulation.


물리 시뮬레이션을 통해서 object의 위치를 제어하는 컴포넌트.


Adding a Rigidbody component to an object will put its motion under the control of Unity's physics engine. Even without adding any code, a Rigidbody object will be pulled downward by gravity and will react to collisions with incoming objects if the right Collider component is also present.


Rigibody component를 object에 추가함으로써 유니티의 물리 엔진으로 object의 모션을 제어할 수 있도록 한다. 이 컴포넌트를 추가만 하면 코드를 한 줄도 쓰지 않아도 중력 법칙이 적용되고 다른 object와의 충돌효과도 나타난다. 


The Rigidbody also has a scripting API that lets you apply forces to the object and control it in a physically realistic way. For example, a car's behaviour can be specified in terms of the forces applied by the wheels. Given this information, the physics engine can handle most other aspects of the car's motion, so it will accelerate realistically and respond correctly to collisions.


Rigidbody의 API는 object에 force가 작용하게끔 해주고 조금 더 현실적으로 물리 법칙을 제어할 수 있도록 도와준다. 예를 들어서 자동차의 가장 특징적인 힘인 바퀴에 작용하는 힘도 물리 엔진이 이런 특성을 이해하고 좀 더 현실적으로 충돌에 반응 할 수 있도록 해준다.


In a script, the FixedUpdate function is recommended as the place to apply forces and change Rigidbody settings (as opposed to Update, which is used for most other frame update tasks). The reason for this is that physics updates are carried out in measured time steps that don't coincide with the frame update. FixedUpdate is called immediately before each physics update and so any changes made there will be processed directly.


스크립트에서는 FixedUpdate 함수를 사용할 것을 추천한다. Rigidbody setting을 바꾸고 물리적인 힘을 적용하기 위해서이다. Update 함수가 아니라 FixedUpdate 함수가 사용되는 이유는 물리적 변경은 동시에 일어나는 것이 아니라 정해진 time steps의 frame update에 일어나기 때문이다. FixedUpdate 함수는 물리적 변경이 일어나기 전에 call 되어서 어떠한 변경이든 즉각적으로 반영되도록 도와준다.

(쉽게 말해서 Update는 매 프레임이 계속 갱신 되면서 반영되는 것이고 FixedUpdate는 예정된 프레임에, 반영의 필요가 발생된 시점에 call 되기 때문에 자원의 활용 측면에서 더 효율적이라서 FixedUpdate를 쓴다는 의미로 예상된다)



Rigidbody.AddForce


public void AddForce(Vector3 force, ForceMode mode = ForceMode.Force);


Description

Adds a force to the Rigidbody.


Rigidbody에 force를  반영한다.


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.


Force는 force vector의 방향을 따라 지속적으로 작용한다. ForceMode를 명확히 파악하고 반영하는 것은 충돌이나 속도변화, 가속이 가능하게끔 해준다.


Force can be applied only to an active Rigidbody. If a GameObject is inactive, AddForce has no effect.


Force는 오직 Rigidbody가 활성화 된 경우에만 작동하고 Rigidebody가 inactive인 경우에는 AddForce는 아무런 효과가 없다.



Input.GetAxis


public static float GetAxis(string axisName);


Description

Returns the value of the virtual axis identified by axisName.


axisName에 의해 확립된 virtual axis 값을 반환한다


The value will be in the range -1...1 for keyboard and joystick input. If the axis is setup to be delta mouse movement, the mouse delta is multiplied by the axis sensitivity and the range is not -1...1.


키보드와 조이스틱의 입력에 따라 -1 ~ 1 사이의 값으로 매겨진다. 델타 마우스 무브먼트에 따라 setup 될시에 이 레인지 값은 -1 ~ 1이 아니다.





// 의도 : Inputmanager의 키 입력에 따라 player object의 위치를 변경(움직이는 효과)하고자 함

public class PlayerController : MonoBehaviour {

public float speed;
// public 변수 생성시 Inspector 창에 뜨게 되어서 Unity interface에서
// 관리가 가능하게 된다. 이렇게 되면 수정시 매번 스크립트를 변경하지 않아도 되고
// 바로바로 수치 변경과 반영이 용이해진다.

    private Rigidbody rb;
    // 위치 반영과 충돌, 가속 등을 오브젝트에 반영하기 위해서 선언

    void Start () {
    
    rb = GetComponent<Rigidbody>();
    // Rigidbody component의 모든 정보를 rb에 초기화
    }

    void FixedUpdate () {
    // frame rate에 상관없이 주기적으로 타이머 형식으로 호출되는 함수
// 주로 Rigidbody의 AddForce 반영시 사용

        float moveHorizontal = Input.GetAxis("Horizontal"); // 키보드 입력에 따른 수평축 값 호출
        float moveVertical = Input.GetAxis("Vertical"); // 키보드 입력에 따른 수직축 값 호출

Vector3 movement = new Vector3(moveHorizontal, 0.0F, moveVertical);
// 위치 좌표 반영을 위해서 Vector3 객체 생성하고 초기화 바로 실시
// 판 위에서 구를 것이기 때문에 높이 변경이 필요 없어서 0.0F 값으로 들어간다

rb.AddForce(movement * speed);  
        // 일정 타이머에 맞춰서 지속적으로 실시되는 FixedUpdate에 AddForce 함수를 넣음으로 써
// 위치 반영이 키보드 입력에 따라 반영될 수 있게 된다
        }
}




// 의도 : 카메라가 특정 오브젝트를 따라다니면서 촬영하게 만들어준다.

public class CameraController : MonoBehaviour {

public GameObject player;
// Inspector에 바로 뜨게 만들어서 거기에 Player 오브젝트를 집어 넣기 위해
// 스크립트에서 public 설정하여 선언한다

private Vector3 offset;
// 상쇄 값 만들기 위해서 선언한다

    // Use this for initialization
    void Start () {
offset = transform.position - player.transform.position;
        // 상쇄할 값을 초기화 한다
// trasform.position은 이 스크립트 컴포넌트의 모체의 위치값이고
// player.transform.position은 player에 할당 될 object의 위치값이다
// 즉, offset은 이 스크립트의 모체 위치와 player의 위치 간의 거리 값이 된다
    }
    
    // Update is called once per frame
    void LateUpdate () {
// LateUpdate는 Update함수의 실행 뒤에 뒤따라 실행되는 함수이다
transform.position = player.transform.position + offset;
// transform.position을 LateUpdate 함수가 실행되는 매 프레임에 갱신하게 된다
    }
}


'Game > Unity' 카테고리의 다른 글

Roll a ball -2  (0) 2018.01.08
Comments