상세 컨텐츠

본문 제목

유니티 플레이어 3D 캐릭터 방향전환(rotate,rotation)

유니티

by MJ_119 2023. 7. 12. 22:02

본문

public class MovePlayer : MonoBehaviour
{

    public Transform tr;
    public float rotSpeed = 150.0f;
    
    void Start()
    {
        tr = GetComponent<Transform>();
    }

	void Update()
    {
        float currentY = tr.rotation.eulerAngles.y;
        
        // 오른쪽 회전
        if (Input.GetKey(KeyCode.E))
        {
            // 로컬 기준
            // tr.Rotate(Vector3.up * Time.deltaTime * rotSpeed, Space.Self);

            // 월드 기준
            // tr.Rotate(Vector3.up * Time.deltaTime * rotSpeed, Space.World);

            tr.rotation = Quaternion.AngleAxis(currentY + Time.deltaTime * rotSpeed, Vector3.up);
        }
        
        // 왼쪽 회전
        else if (Input.GetKey(KeyCode.Q))
        {
            // 로컬 기준
            // tr.Rotate(Vector3.down * Time.deltaTime * rotSpeed, Space.Self);

            // 월드 기준
            // tr.Rotate(Vector3.down * Time.deltaTime * rotSpeed, Space.World);

            tr.rotation = Quaternion.AngleAxis(currentY - Time.deltaTime * rotSpeed, Vector3.up);
        }
    }
}

관련글 더보기