- Home /
Rotate towards target doesn't work properly (Quaternion.lerp)
Hello,
I wrote a simple "Rotate towards target" script and was testing it but for some reason the object that I rotate ends up upside down.
Here is the code: (javascript)
#pragma strict
var target : Transform;
var strength = .5;
var targetRotation : Quaternion;
var str : float;
function Update () {
targetRotation = Quaternion.LookRotation (target.position - transform.position);
str = Mathf.Min (strength * Time.deltaTime, 1);
transform.rotation = Quaternion.Lerp (transform.rotation, targetRotation, str);
}
and here is the picture of what is going wrong:
The ship needs to rotate with the front towards the planet in the background but ends up with the front down. I already changed the pivot/origin point in my 3d software (blender) but that doesn't change a thing...
How do I fix this?
Thanks in advance,
Gijs
Answer by robertbu · Aug 09, 2014 at 06:37 PM
For LookRotation() to work, the front of your object must face positive 'z' and up face positive 'y' when the rotation of the object is (0,0,0). This can be fixed in the modeling program, or you can use an empty game object as a parent and rotate the visible child object.
Thanks I fixed the model! I really don't know how I could miss that... dumb me :D
Your answer
Follow this Question
Related Questions
Slerp, lerp or something else? 3 Answers
Limiting Camera Movement 1 Answer
Rotate object using mouse, slows down on release 2 Answers
Rotating one axis using Mathf.Lerp 1 Answer