public static float Atan2 (float y, float x);

Description

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

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

Note: This function takes account of the cases where x is zero and returns the correct angle rather than throwing a division by zero exception.

// 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); } }