본문 바로가기
게임개발/게임플랫폼 응용 프로그래밍

Mathf.Atan2

by pudding81 2024. 3. 6.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Woman : MonoBehaviour
{
    void Update()
    {
        float h = Input.GetAxisRaw("Horizontal");
        float v = Input.GetAxisRaw("Vertical");
        Vector3 dir = new Vector3(h, 0, v);
        Debug.Log(dir);

        float angle = Mathf.Atan2(dir.x, dir.z) * Mathf.Rad2Deg;
        //축으로 회전 
        transform.rotation = Quaternion.Euler(new Vector3(0, angle, 0));

    }
}

플레이어가 특정 방향(dir)을 바라보도록 회전

Mathf.Atan2 메서드는 두 벡터의 각도를 반환합니다. 이 값을 Mathf.Rad2Deg를 곱하여 라디안에서 각도로 변환합니다.
그리고 Quaternion.Euler 메서드를 사용하여 오일러 각도로 쿼터니언을 생성합니다.
마지막으로 transform.rotation에 쿼터니언을 할당하여 플레이어의 회전을 변경합니다.



https://en.wikipedia.org/wiki/Atan2

atan2 - Wikipedia

From Wikipedia, the free encyclopedia Arctangent function with two arguments atan2(y, x) returns the angle θ between the positive x-axis and the ray from the origin to the point (x, y), confined to (−π, π]. Graph of atan2 ⁡ ( y , x ) {\displaystyle

en.wikipedia.org