- Home /
3D nested Turret Prefab Rotation
Hi I have a system where a ship has a turret attached to it, the ship has full 3d movement and the turret is comprised of 2 parts as shown in the Hierarchy image below.
The base should just rotate via yaw and the head which should use both yaw and pitch [should look like its just pitching since base yaw = head yaw] currently it works perfectly except when the ship rotates about the Z axis [roll] when the turret base and head go out of alignment with the surface they are "attached" to and gain extra roll which doesn't look right.
I'm sure its to do with my rotation code [attached to the "1SML-TwinTurret" object] but I cant seam to work out the correct method to fix it. currently I'm using this.
Vector3 direction = Vector3.Normalize (target.position - transform.position);
Vector3 basetarget = new Vector3 (direction.x, 0, direction.z);
turretbase.rotation = Quaternion.RotateTowards (turretbase.localRotation, Quaternion.LookRotation (basetarget), turnspeed * Time.deltaTime);
turrethead.rotation = Quaternion.RotateTowards (turrethead.localRotation, Quaternion.LookRotation (direction), turnspeed * Time.deltaTime);
I'm sure there's a better way to do this but I cant seam to work it out, ideally I want to have both the head and the base's roll the same as the ships and them to rotate at a fixed speed towards its target.
Any help would be appreciated
Answer by Lord Simpson · Apr 03, 2011 at 12:38 PM
Ok think ive fixed my own problem again :D
turrethead.rotation = Quaternion.RotateTowards (turrethead.rotation,Quaternion.LookRotation(direction,transform.up), turnspeed * Time.deltaTime);
Vector3 rot = turrethead.localEulerAngles;
rot.x = 0; rot.z = 0;
turretbase.localEulerAngles = rot;
Realised all I had to do to fix the roll problem was use add transform.up to the LookRotation [didn't realise it had that option...]
The base was being problematic but then I realised I could just lock it to the head's rotation and it seams to be passing all my test.