Issue with rotating Child Objects on a single access

I’m currently working on a “spaceship” game experiment where the ships move on the X and Z axis in a sort of game board fashion. Each ship has a few turrets that are mounted either on the side(s) or top of the craft and only need to rotate towards their target on a single axis and are located fairly deep in the child hierarchy of the ship.

I found some code out there that gets me part of the way there but the problem I’m seeing is that if the ship (root transform) is rotated in any way on the Y axis then the turret rotations are thrown off and no longer point directly at their target.

var newRotation = Quaternion.LookRotation(owner.current_target.transform.position - gun_emplacement.position, transform.forward);
            newRotation.z = 0;
            newRotation.y = 0;
            newRotation.x *=  (slot.orentiation == Location.Left) ? 1 : -1;


            dot_angle = Vector3.Dot(gun_emplacement.position.normalized, owner.current_target.transform.position.normalized);
            gun_emplacement.localRotation = Quaternion.SlerpUnclamped(gun_emplacement.localRotation, newRotation, Time.deltaTime * 8);

I’ve tried a number of things in order to find a solution that works for all rotations of the parent and I’m pretty stumped at this point. There’s probably something going on in the parent rotation (And position ?) I’m not accounting for and I have to admit my math skills are pretty dodgy :stuck_out_tongue:

Any ideas on this one?

Rotating an object will necessarily rotate all child objects accordingly. If you want the (child) turrets to continue pointing towards a particular target, then, after rotating the ship, you would need to call turret.transform.LookAt( target ).

Ah. I’ll give that a shot! Thank you :slight_smile:

I think it is worth noting that each turret does have their orientation adjusted in their update statement(s) and I would expect that as the ship itself rotates, that update statement should take care of the child turret rotation. Some adjustment does occur as the ship rotates, it’s just in the wrong direction.

I just tried your solution and it worked for keeping the turret aimed at the target in a reasonable way so thank you for that. I’m not all that happy with the other angles of rotation of the turret but I am not going to worry about that so much :slight_smile:

Appreciate the help!

1 Like

If it isn’t looking directly at the target each frame, then either the ship or the target (or both) could be being moved after the call to LookAt is made.

Glad you were able to resolve the problem. :slight_smile: