- Home /
aim at an object that approches
how do i make a turrent aim at an object? and on top of that how do you make the turrent look when the player approches?
Answer by Jace · Jan 02, 2012 at 12:59 AM
Try using transform.LookAt which will rotate the object in the direction of a given target
http://unity3d.com/support/documentation/ScriptReference/Transform.LookAt.html
Answer by aldonaletto · Jan 02, 2012 at 01:09 AM
You can use transform.LookAt(target) to make the turret point to the target transform, and the target may be set in the Inspector or at Start with FindWithTag:
var target: Transform; // drag target here or let Start find it var range: float = 25; // distance the target can be found
function Start(){ if (!target){ target = GameObject.FindWithTag("Player").transform; } }
function Update(){ if (Vector3.Distance(target.position, transform.position) < range){ transform.LookAt(target); } } NOTE: You must tag the target as "Player" if you want Start to find it automatically.