Hi I have a setup where a turret is made up of two gameobjects, one being the base that should only rotate on the Y axis and a head which is a child of the base which should only rotate in the X axis… The problem im experiencing is that to restrict the base to the y axis i convert the Quterion rotation of the head into euler angles which seams to either cause a loss of accuracy or for some other reason instead of facing a moving target it aims just to one side then next frame just a little to the other side
turrethead.rotation = Quaternion.RotateTowards (turrethead.rotation, Quaternion.LookRotation (gooddirection, transform.up), turnspeed * Time.deltaTime);
Vector3 rot = turrethead.localEulerAngles;
rot.x = 0;
rot.z = 0;
turretbase.localEulerAngles = rot; [commenting out this line fixes things]
what I think I need is instead of using Euler angles is to restrict the rotation using pure quaternion maths but cant seam to find anything on how to remove the “x and z” rotation from a quaternion
Any help would be appreciated
OK simplified things…was thinking to much in reality. The Most important thing is the head faces the target and really the base is just graphical fluff so removed the parenting and used
turrethead.rotation = Quaternion.RotateTowards(turrethead.rotation,Quaternion.LookRotation(gooddirection,transform.up),turnspeed*Time.deltaTime);
turretbase.localEulerAngles = new Vector3(0,turrethead.localEulerAngles.y,0);
Quaternions don't work in 3 axis like the eulerangles representation. It's more a combination of an internal vector3 that specifies a rotation axis and the rotation around this axis.
Your problem is that you rotate the head before the base and you rotate both objects in worldspace. So rotating the head towards the target in worldspace and after that rotating the base would also rotate the head since it's a child of the base. Therefore your head points into the wrong direction which causes the procedure to repeat each frame.
Why not use one of the Transform.Rotate() variants to rotate around just one axis?
Say for example:
//This will rotate the turrethead by 90 degrees around the x axis
turrethead.Rotate(90.0f, 0.0f, 0.0f);
//This will rotate the turret base by 45 degrees around the y axis
turretbase.Rotate(0.0f, 45.0f, 0.0f);