프로그래밍/Unity C#
[Unity] Tutorial 05 - Beginner Gameplay Scripting (Rotate)
김잉장
2019. 7. 3. 20:30
목표
- 물체의 회전 방법에 대해 알아보자.
Rotate
- unity에서 기본적으로 물체를 회전 시키는 방법은 transform의 Rotate를 사용하는 것이다.
- 회전은 종종 쿼터니언(Quaternion)이 아닌 오일러 각(Euler angle)으로 제공된다.
- 월드 축(world axes) 이나 로컬 축(local axes)에서 회전을 지정할 수 있다.
- 다음 예제에서 Transform.Rotate에 대해 알아보자.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TransformFunctions : MonoBehaviour
{
public float MoveSpeed = 10;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
Debug.Log("reset!!");
// for reset local rotation
this.transform.localRotation = Quaternion.Euler(new Vector3(0, 0, 0));
// for reset world rotation
this.transform.rotation = Quaternion.Euler(new Vector3(0, 0, 0));
}
if (Input.GetKey(KeyCode.Alpha1))
{
// 물체가 로컬의 X,Y축ㅇ 기준으로 회전합니다.
Vector3 eulerAngles = Quaternion.Euler(1, 1, 0).eulerAngles;
this.transform.Rotate(eulerAngles);
}
if (Input.GetKey(KeyCode.Alpha2))
{
// 물체가 월드의 Y축을 기준으로 회전합니다.
this.transform.Rotate(Vector3.up, Space.World);
}
if (Input.GetKeyDown(KeyCode.Alpha3))
{
// 물체가 월드의 X,Y축을 기준으로 회전합니다.
Vector3 eulerAngles = Quaternion.Euler(1, 1, 0).eulerAngles;
this.transform.Rotate(eulerAngles, Space.World);
}
if (Input.GetKeyDown(KeyCode.Alpha4))
{
// 물체가 Y축을 기준으로 15도씩 회전합니다.
this.transform.Rotate(Vector3.up, 15.0f);
}
if (Input.GetKey(KeyCode.Alpha5))
{
// 물체가 로컬의 x, y, z 축ㅇ로 회전합니다.
this.transform.Rotate(0.0f, 0.0f, 1.0f);
}
if (Input.GetKey(KeyCode.Alpha6))
{
// 물체가 월드의 x, y, z 축으로 회전합니다.
this.transform.Rotate(0.0f, 1.0f, 1.0f, Space.World);
}
}
}