- Home /
Tilting with transform.localEulerAngles and Vector3.Lerp not working.
Hey guys, and thanks in advance for reading my question.
I've got the following problem: I've got a transform with a child and I want to rotate the child over the Z axis, based on the parents rotation on the Y axis (as if an airplane is tilting). Now I try this with the following bit of code:
NewY = transform.localEulerAngles.y;
rotationZ = -Mathf.Clamp((NewY - OldY) * 30, -90, 90);
playerChild.transform.localEulerAngles = Vector3.Lerp(playerChild.transform.localEulerAngles,
new Vector3(playerChild.transform.localEulerAngles.x, playerChild.transform.localEulerAngles.y, rotationZ), 0.1F);
OldY = NewY;
This is all done in the regular Update and playerChild is a GameObject which is not null, because it's working for some reason, but only when the rotationZ is positive. As soon as rotationZ becomes negative, my child object starts doing 360's over it's Z axis. I don't know if there's some rule to Vector3.Lerp and not working well with negative values? I've never encountered this issue before, so any help is welcome!
Reading from eulerAngles is not a good idea. They change representation. For more information and a suggested solution, read the eulerAngles section (last section) of this answer:
http://answers.unity3d.com/questions/799824/oafatwhy-does-transformrotationx-45-not-work.html
Thanks for your quick response. Though I found the solution myself your article (and the links in it) was very helpfull. Thanks!
Answer by Malfegor · Oct 03, 2014 at 10:54 AM
Fixed it! Apparently it was the Lerp that was bugging. In the end I used Mathf.SmoothDampAngle to achieve what I wanted to get.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Vector3.Lerp moves in the wrong direction 1 Answer
Camera Lerp from A to B, and back 2 Answers