쫑가 과정

타겟 방향 바라보기 함수 / Quaternion.LookRotation 본문

Unity/이론

타겟 방향 바라보기 함수 / Quaternion.LookRotation

쫑가 2022. 1. 12. 19:49

1. Quaternion.LookRotation


https://docs.unity3d.com/kr/530/ScriptReference/Quaternion.LookRotation.html

 

Unity - 스크립팅 API: Quaternion.LookRotation

계산된 쿼터니언을 반환합니다. If used to orient a Transform, the Z axis will be aligned with forward/ and the Y axis with upwards if these vectors are orthogonal. Logs an error if the forward direction is zero.

docs.unity3d.com

public static Quaternion LookRotation(Vector3 forward, Vector3 upwards = Vector3.up);
// foward = 정면방향, upward = 위쪽방향을 지정

 

지정된 앞(forward)과 위(upwards) 방향(directions)으로 회전(rotation)을 만든다.

@ forward방향은 바라보고 있는 (정면)방향 / upward방향은 위쪽 방향.

설명 : Z축은 정방향과 정렬되고, X축은 정방향과 위쪽 사이의 외적과 정렬되고, Y축은 Z와 X 사이의 외적과 정렬

예시) 타겟 방향으로 정면 회전

using UnityEngine;

public class PlayerRotate : MonoBehaviour
{
    public Transform target;
    void Update()
    {
        Vector3 relativePos = target.position - transform.position;
        Quaternion rotation = Quaternion.LookRotation(relativePos);
        transform.rotation = rotation;
    }
}

나만을 바라본다!

Comments