- Home /
Why is transform.LookAt backwards for my model?
I have an animated character I bought from the asset store. When I do the LookAt he looks exactly in the same direction that Im looking at, the result is his back is to me. The second line below works though, but, I would rather not have to do that hack and figure out what the problem is, any idea?
//back facing
transform.LookAt(target.transform);
//this works
transform.rotation = Quaternion.LookRotation(transform.position - target.transform.position);
So the model's forward is facing towards the negative Z-Axis? Unless you fix the model in a modelling program, or use an empty gameObject as a container for the model, you are stuck and have to work with your inverted LookRotation. (LookAt is not broken!)
Answer by Kryptos · Nov 12, 2012 at 09:30 AM
The fact that the second code works, show that your object is facing towards the negative z-axis.
The equivalent of transform.LookAt(target.transform)
is Quaternion.LookRotation(target.transform.position - transform.position)
.
Thus you should fix your model. For example you can create a parent GameObject and add a 180-degree rotation around the y-axis.
Answer by deltamish · Nov 12, 2012 at 07:16 AM
Updated
given reason for change in the script and why the problem occurs
Anyways
Use this script to make the character look towards you
public var target:Transform;
public var rotationspeed:float = 2.0;
function Update(){
var direction:Vector3 = target.position - transform.position;
transform.rotation = Vector3.Slerp(transform.rotation,Quaternion.LookRotation(direction),rotationspeed * Time.smoothDeltaTime);
transform.eulerAngles = Vector3(0,transform.eulerAngles.y,0);
}
NOTE You script has direction = transform.position - target.transform.position; this is causing your object to turn away.(This is like giving negative values)
Where as mine has target.position(you can also use target.transform.position) - transform.position;this makes the body to turn towards
the player or any object.(This is like giving positive values)
What do I mean? "He looks away from me". Also, how is all that code better than the 1 line of $$anonymous$$e that actually works, and, this doesn't help me solve the problem as to why "LookAt" is broken in my use case.
Cause transform.lookAt makes the object turn quickly whereas my code makes it turn smooth and slowly(more realistic).Try it just copy mode or edit your self
if you want the object to trun a bit more quickly use Time.deltatime or change the rotation speed values
Cause transform.lookAt makes the object turn quickly
It does not "turn" quickly, it sets the rotation in one frame. Anyway the issue here is not related to the speed of the rotation but to correct the facing direction.
Your answer is interesting but off-topic.
I have given answer to his rotation problem in the note section Sorry for my bad comment.i commented about rotation speed because He asked why did i replace transform.LookAt with my code. The script works , it makes the object to rotate towards the player or any object.