Mathf.Atan2 反正切2

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

Description描述

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).

返回值为x轴和一个零点起始在(x,y)结束的2D向量的之间夹角。

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.
//通常使用transform.lookAt.
// 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