본문 바로가기

Unity/Input

[Unity] 캐릭터 좌우로 움직이기 (RigidBody)

[Unity] 캐릭터 좌우로 움직이기 (RigidBody)

 

 

[Header("속도 관련 변수")][Range(0,1)]
[SerializeField] float moveSpeed;
//강제로 인스펙터 창에 띄울 수 있음
 
Rigidbody myRigid;
// Update is called once per frame
void Update () {
 
    if (Input.GetAxisRaw("Horizontal") != 0) // 왼쪽 -1, 오른쪽 1
    {
        Vector3 moveDir = new Vector3(0, 0, Input.GetAxisRaw("Horizontal"));
        myRigid.AddForce(moveDir * moveSpeed);
    }
}