Mathf.Atan2 Manual     Reference     Scripting  
Scripting > Runtime Classes > Mathf
Mathf.Atan2

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

Description

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

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

// Usually you use transform.LookAt for this.
// But this can give you more control over the angle
var target : Transform;
function Update ()
{
var relative = transform.InverseTransformPoint(target.position);
var angle = Mathf.Atan2(relative.x, relative.z) * Mathf.Rad2Deg;
transform.Rotate (0, angle, 0);
}