Setting Child's Rotation To Constant Global Rotation

I have a tank, which is a empty game object that has 2 child objects; turret and body. I need to make it so that when the parent rotates, the turret's local z rotation remains the same from a global viewpoint, until it is changed by the MouseLook script that is on the turret.

Here are two images to help describe this problem:

What i want it to do

What is happening

How can I do this? Thanks in advance.

2 Answers

2

A quick solution would be to simply find out how much the empty parent object is rotating, and move the turret gameObject negative the same amount.

Another is to have the turret not actually a child of the tank, but have a replacement empty gameobject in the turrets location and cache the actual turret gameobject, then set it's position to that of the empty replacement object which would be following the tank.

Theres some other more elegant solutions like setting the turrets rotation to a fixed angle during update with the following, but I won't get too in-depth with that as I am quite a newbie myself.

transform.rotation = Quaternion.identity;

Take your pick.

Will Test Soon, your first two answers seem like they will work.

Just to correct this old answer: rotation (and eulerAngles) are in global space anyway:

transform.eulerAngles = Vector3(x,y,z);

…unless you choose to use parent-relative local rotation:

transform.localEulerAngles = Vector3(x,y,z);