- Home /
GameObject facing fowards
I am moving a transform object but i need it to always face forwards. Any help appreciated.
function Update () { if (activeTrail.length < 1) // If we have no positions in the trail, we have nothing to do in Update { return; }
transform.position = Vector3.Lerp (transform.position, activeTrail [0], Time.deltaTime * speed);
// Move towards the next position in the trail
if (Vector3.Distance (transform.position, activeTrail [0]) < nearDistance)
// If we have arrived at the next position in the trail, then...
{
activeTrail.Shift ();
// ... remove that position from the trail - making the next on the list our new destination
UpdateLine();
}
}
Answer by Thomas Hentschel Lund · Jun 24, 2010 at 11:36 AM
You could use the transform.lookat() to easily have it face some specific world point - for example your next point on the trail
i have tried this forwardControl = activeTrail[0]; transform.LookAt(forwardControl);
but get an error: Assets/new touch scripts/Draggable.js(34,20): BCE0004: Ambiguous reference 'LookAt': UnityEngine.Transform.LookAt(UnityEngine.Vector3), UnityEngine.Transform.LookAt(UnityEngine.Transform).
You need to declare the type you're sending; Unity doesn't know what function to call with an ambiguous var like that (untyped element of an array = ambiguous). Try LookAt( forwardControl as Vector3 )