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
Any ideas on this one?