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

5071-image002.png

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.

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.