I’ve been working at a problem with some time, and a week+ of googling and experimenting has got me nowhere.
–THE PROBLEM–
I have a turret on a moving object (let’s say, for the sake of simplicity, it’s on a tank).
Now the turret faces the mouse position with limitations:
- It cannot rotate left or right so that the angle is in excess of variable maxLRArc/2.
- It is limited in L/R rotation speed by rotationSpeedLR
- It cannot rotate up or down so that the angle is in excess of variable maxUDArc/2.
- It is limited in U/D rotation speed by rotationSpeedUD
Here’s a picture for clarity:
EDIT: A picture of the scene in Unity:

- The YELLOW line is the raytrace from camera to mouse position
- The CYAN lines form the limits of the Up/Down rotation
- The RED (less visible) lines form the limits of the Left/Right rotation
- The BLUE line is the mouse position projected on the L/R plane
- The MAGENTA line is the mouse position projected on the U/D plane
- The GREEN line shows the parent’s forward vector from the position of the turret
If it’s still unclear you can think about it as having someone’s head track a moving object… The angle limitations are apparent in that scenario… but the person can only turn his head left or right and pitch up or down, but not tilt his head to one side or another.
–THE APPROACH–
I’ve been attempting to approach the problem from a step-by-step method, which is to say:
- Turret first rotates left or right towards the mouse.
- Turret then rotates up or down towards the mouse.
The way I’m doing is is first projecting the mouse position vector onto a plane in-line with the turret. Once for each step of the rotation, so that the mouse position is in-plane with the step’s angle limits.
The checks and balances all work beautifully, the problem is executing the rotations, WITH a constant speed.
When I attempted to rotate for each axis separately, the rotations interfered with one another. When I attempted to rotate them together, the turret ended up twisting along the local Z axis (separating from it’s logical point-of-contact to the tank) particularly noticeable when the tank goes up or down an incline.
–THE PLEA–
I’ve tried a lot of solutions that I’ve found but none give the desired results. Most (if not all) deal with a few of the restrictions I’ve listed individually, but not together like this.
I figure I’m at the limits of my capability and need to yield to more experienced individuals. Any help is greatly appreciated!
EDIT: The code I have is so messy and fragmented that it would be more harm than good, but if necessary, i can provide it.
