쫑가 과정

두 회전 사이의 각 구하기 / Quaternion.Angle 본문

Unity/이론

두 회전 사이의 각 구하기 / Quaternion.Angle

쫑가 2022. 1. 12. 20:38

2. Quaternion.Angle


https://docs.unity3d.com/ScriptReference/Quaternion.Angle.html

 

Unity - Scripting API: Quaternion.Angle

Success! Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable. Close

docs.unity3d.com

public static float Angle(Quaternion a, Quaternion b);

a 와 b 두 회전 사이의 각도(degrees)를 도(angle) 단위로 반환한다.

 

예를 들어 세 번째 게임 오브젝트(C) 주위를 움직이는 두 개의 게임 오브젝트(A 및 B)를 생각해보자.

C에서 A로, C에서 B로의 선은 시간이 지남에 따라 변할 수 있는 삼각형을 만든다.

결과적으로 CA와 CB 사이의 각도는 Quaternion.Angle이 제공하는 값이다.

 

예)

using UnityEngine;
using System.Collections;

// Calculates the angle (degrees) between
// the rotation of this transform and target.

public class ExampleClass : MonoBehaviour
{
    public Transform target;

    void Update()
    {
        float angle = Quaternion.Angle(transform.rotation, target.rotation);
        Debug.Log(angle);
    }
}

쉽게 2D로 보자! 하얀 cube가 target이다.

1. angle = 90f 

transform.rotation = Quaternion.Euler(new Vector(0,0,90f));
target.transform.rotation = Quaternion.Euler(new Vector(0,0,0));

2. angle = 60f (근사값으로 나옴)

transform.rotation = Quaternion.Euler(new Vector(0,0,90f));
target.transform.rotation = Quaternion.Euler(new Vector(0,0,30f));

3. angle = 120

transform.rotation = Quaternion.Euler(new Vector(0,0,120f));
target.transform.rotation = Quaternion.Euler(new Vector(0,0,0));

Comments