Mathf.Atan2 反正切2

static function Atan2 (y : float, x : float) : float


Returns the angle in radians whose Tan is y/x.

以弧度为单位计算并返回 y/x 的反正切值。返回值表示相对直角三角形对角的角,其中 x 是临边边长,而 y 是对边边长。

Return value is the angle between the x-axis and a 2D vector starting at zero and terminating at (x,y).


using UnityEngine;
using System.Collections;

public class example : 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);
// Usually you use transform.LookAt for this.
// But this can give you more control over the angle

var target : Transform;

function Update () {
	var relative : Vector3 = transform.InverseTransformPoint(target.position);
	var angle : float = Mathf.Atan2(relative.x, relative.z) * Mathf.Rad2Deg;
	transform.Rotate (0, angle, 0);
Page last updated: 2013-4-8