- Home /
Rotation Problem, if Mesh gets a Parent
Hello everybody!
After spending hours with no results, I'm asking here for help. I have came across the problem, that I wanted to limit the rotation of a Canon to an Axis. For example on a Tank or on my Tower.
The funny thing is, if I write a simple Script with Euler-Rotation it works like a charme, but if I parent the cube, that should be obviously the cannon, to my Tower (precisely on the Pivot-Object), the hell breaks loose.
See the picture below. The right and the left Cube, have the same setup, except that the left one has the Tower, as its parent.
Here the Hierarchy
Before the cube, I use a fixated point for the rotation, so the cube rotates around its parent and not its center.
On top of that, I found another strange problem. If I change the cylinders position and select its parent. Gizmos shows me its location always on the center of the mesh, even tough the position hasn't changed a bit. Is this a bug? It only happens, when there is a Mesh-Renderer. Without a Mesh, Gizmos displays the position correctly.
The code for rotation (I limit the rotation to 20 degrees around the y-Axis)
// Update is called once per frame
void Update () {
Vector3 look = Quaternion.LookRotation((target.position - rotate.position).normalized).eulerAngles;
if ((look.y > 180 && look.y < 350f)) {
look.y = 350f;
} else if (look.y > 10 && look.y < 180f) {
look.y = 10f;
}
look.z = 0f;
rotate.rotation = Quaternion.Slerp(rotate.rotation, Quaternion.Euler(look), Time.deltaTime*50f);
Debug.DrawLine(rotate.position, rotate.position + rotate.forward * 20f, Color.white);
}
Can someone please explain me this strange happening? Why does it only work without parents, that have also meshes? And in this example they don't even change its pivot-Angle, so the euler-Angles must be fine?
I'll attach the example on an answer.
Here my problem about Gizmos http://goy.bplaced.net/data/image003.png
and here the example http://goy.bplaced.net/data/testScene.unitypackage
Here you can see my functional Tower, but the cannons still rotate independendly of the Pivot-Object. http://www.youtube.com/watch?v=OVlUd1HNFpo
Answer by sparkzbarca · Nov 19, 2012 at 09:34 PM
http://www.ogre3d.org/tikiwiki/Euler+Angle+Class
The primary problem is that multiple combinations of euler angles can produce the same quaternion or matrix. This means that converting eulers into a quaternion then back to euler can give different (but equivilant) results than what you started with. This is often a problem when attempting to apply constraints to an orientation (such as limiting a camera's pitch).
you'll notice your trying to apply constraints to an orientation and converting eulers to quaternions and back.
Long story short you constant converting between quanternions and eulers is the problem. Pick a type of rotation and stick with it.
Your link was helpful and I was afraid it would be something like this. But if I can't use the Euler-Angles, how am I supposed to limit the rotation to one axis? I only know the Rotate(axis, angle) $$anonymous$$ethod which adds to the already applied angle, but I need to set a calculated angle or limit it to a $$anonymous$$- and maximum range of degrees, it can rotate.
The only possibility I know to do that is, having another Vector-Direction with the original forward and the new Direction its facing to and call Vector3.Angle.