- Home /
My look at script not working?
I have an enemy script I created. All it is suppose to do is make the enemy look at the target. That part works but when I move the target the rotation stays the same. How can I fix this? Here is my code.
var Distance;
var Target:Transform; //This is the target, it is a prefab if that makes a difference
var LookAtDistance = 5.0;
var LookTime = 6.0;
function Update()
{
Distance = Vector3.Distance(Target.position, transform.position);
if(Distance > LookAtDistance)
{
Debug.Log("working");
LookAt();
}
}
function LookAt()
{
var rotation = Quaternion.LookRotation(Target.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * LookTime);
}
Comment
I am setting the Target as a Transform, and the transform is the character controller(target).
Your comment says it is a 'prefab'. Are you setting it to a real prefab or are you setting it to an game object in the Scene? It needs to be set to the live object in the scene to work.
Your answer
Follow this Question
Related Questions
Rotation via iTween.LookTo not working (or using it wrong) 1 Answer
LookRotation is Returning Weird Values 0 Answers
Look at wont update? 2 Answers
Camera following/looking at aircraft 1 Answer
Turret Rotation Problems 1 Answer