Returns the angle in radians whose Tan is y/x
.
// 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); }
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); } }
import UnityEngine import System.Collections public class Example(MonoBehaviour): public target as Transform def Update() as void: relative as Vector3 = transform.InverseTransformPoint(target.position) angle as float = (Mathf.Atan2(relative.x, relative.z) * Mathf.Rad2Deg) transform.Rotate(0, angle, 0)