transform.rotation 속성을 통해 관리되며, 이 값은 Quaternion 타입이다.Quaternion은 x, y, z, w의 4차원 값을 사용하여 짐벌 락(Gimbal Lock) 문제를 방지하고 회전을 표현한다.eulerAngles를 사용할 수 있다.transform.eulerAngles = new Vector3(0.0f, _yAngle, 0.0f);
eulerAngles는 Vector3 값을 사용해 X, Y, Z 축 회전 값을 직접 설정할 수 있다.transform.eulerAngles += new Vector3(0.0f, _yAngle, 0.0f); // ❌ 오류 가능성
eulerAngles는 360도를 넘어가면 정확한 계산이 보장되지 않음.transform.Rotate(new Vector3(0.0f, Time.deltaTime * 100.0f, 0.0f));
Rotate(Vector3)를 사용하면 현재 회전값에서 특정 각도만큼 더 회전할 수 있다.rotation은 Transform의 회전값을 Quaternion 형태로 저장하는 속성이다.transform.rotation = Quaternion.Euler(new Vector3(0.0f, _yAngle, 0.0f));
Euler(Vector3)를 사용하여 오일러 각도를 Quaternion으로 변환할 수 있다.transform.rotation = Quaternion.LookRotation(Vector3.forward);
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Vector3.forward), 0.2f);
Slerp(A, B, t): A에서 B까지 t 비율만큼 회전._yAngle += Time.deltaTime * 100.0f;
transform.eulerAngles = new Vector3(0.0f, _yAngle, 0.0f);
_yAngle 값을 매 프레임 증가시켜 Y축 회전을 지속적으로 증가.transform.Rotate(new Vector3(0.0f, Time.deltaTime * 100.0f, 0.0f));
Rotate()를 사용하여 현재 회전값에서 상대적으로 회전.transform.rotation = Quaternion.Euler(new Vector3(0.0f, _yAngle, 0.0f));
Quaternion.Euler를 통해 회전을 설정.transform.rotation = Quaternion.LookRotation(Vector3.forward);
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Vector3.forward), 0.2f);
void Update()
{
if (Input.GetKey(KeyCode.W)) // 전진
{
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Vector3.forward), 0.2f);
transform.position += Vector3.forward * Time.deltaTime * _speed;
}
if (Input.GetKey(KeyCode.S)) // 후진
{
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Vector3.back), 0.2f);
transform.position += Vector3.back * Time.deltaTime * _speed;
}
if (Input.GetKey(KeyCode.A)) // 좌측
{
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Vector3.left), 0.2f);
transform.position += Vector3.left * Time.deltaTime * _speed;
}
if (Input.GetKey(KeyCode.D)) // 우측
{
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Vector3.right), 0.2f);
transform.position += Vector3.right * Time.deltaTime * _speed;
}
}
키 입력 감지
Input.GetKey(KeyCode.W): 키 입력을 감지하여 이동.부드러운 회전
Quaternion.Slerp(transform.rotation, 목표 회전값, 0.2f)을 사용해 회전값을 부드럽게 변경.이동 처리
transform.position += Vector3.forward * Time.deltaTime * _speed;position을 변경하여 이동.