Version: 2020.3
public static float Atan2 (float y, float x);

描述

返回其 Tany/x 的角度(以弧度为单位)。

返回值是 X 轴与 2D 向量(从零开始,在 (x,y) 处终止)之间的 角度。

注意:此函数会考虑 x 为零的情况并返回 正确角度,而不是抛出被零除异常。

// Usually you use transform.LookAt for this.
// But this can give you more control over the angle

using UnityEngine; using System.Collections;

public class ExampleClass : MonoBehaviour { public Transform target;

void Update() { Vector3 relative = transform.InverseTransformPoint(target.position); float angle = Mathf.Atan2(relative.x, relative.z) * Mathf.Rad2Deg; transform.Rotate(0, angle, 0); } }