포스트

섹션2. Transform

위 글은 인프런에 있는 Rookiss님의 [C#과 유니티로 만드는 MMORPG 게임 개발 시리즈] Part3: 유니티 엔진 강의를 듣고 남긴 필기입니다.

Position

transform.TransformDirection(Vector3 direction) : 로컬 공간에서 월드 공간으로 direction 변환

transform.Translate(Vector3 translation) : translation 방향과 거리로 이동

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// TransformDirection : local -> World 좌표계로 변환
// InverseTransformDirection : World -> local 좌표계로 변환

// if (Input.GetKey(KeyCode.W))
//     transform.position += transform.TransformDirection(Vector3.forward * (Time.deltaTime * _speed));

if (Input.GetKey(KeyCode.W))
    transform.Translate(Vector3.forward * (Time.deltaTime * _speed));
if (Input.GetKey(KeyCode.A))
    transform.Translate(Vector3.left * (Time.deltaTime * _speed));
if (Input.GetKey(KeyCode.S))
    transform.Translate(Vector3.back * (Time.deltaTime * _speed));
if (Input.GetKey(KeyCode.D))
    transform.Translate(Vector3.right * (Time.deltaTime * _speed));

Rotation

transform.eulerAngles : 오일러 각도로 나타낸 회전

  • 공식 문서에 의하면, transform.eulerAngles 자체에 += 와 같이 직접 연산을 가하는것을 피할 것을 권장.
    ⇒ 대신 Transform.Rotate()를 사용할 것.

  • 가능하면 transform.eulerAngles = new Vector3();와 같은 방식으로 대입하는 방식을 활용할 것.

Quaternion.Euler(float x, float y, float z) : 오일러 각도를 Quaternion으로 변환해주는 함수

Quaternion.LookRotation(Vector3 forward, Vector3 upwards = Vector3.up) : forward를 향하는 Quaternion을 만들어주는 함수

e.g.) Vector3.forward, Vector3.back

Quaternion.Slerp(Quaternion a, Quaternion b, float t) : ab를 구형으로 보간한 Quaternion 을 반환하는 함수. t는 0~1 사이의 비율

Input Manager

1
2
3
4
5
6
7
8
9
10
11
12
13
public class InputManager
{
    public Action KeyAction = null;

    public void OnUpdate()
    {
        // 아무런 입력이 없다면 패스
        if (Input.anyKey == false) return;
        // 그게 아니라면,
        if (KeyAction != null)
            KeyAction.Invoke();
    }
}

입력을 처리할 Input Manager 스크립트의 기본 형태

이 기사는 저작권자의 CC BY 4.0 라이센스를 따릅니다.