Flipping the axis when aiming at a target

Hello everyone How do I make sure that the axis doesn’t flip when hovering over the target? If I position the object and target in the usual way, and rotate the target around the object in the X-Z plane, then everything is fine.

2024-07-31 23-15-52

But when I move the target in the Y-Z plane around the object, then at some point it flips.

2024-07-31 23-17-36

How can I fix this? Why is this happening?

To hover, I use the LookAt command.
I also tried the angle and set only one rotation angle for the object, but it still turns over.

By default LookAt will try to keep the up/green axis pointing upwards. Instead try this:

transform.LookAt(target, transform.up);

Or perhaps you don’t mind the up/green axis always trying to point upwards and its just the instant flipping that you don’t like. If so, try this:

transform.rotation=Quaternion.RotateTowards(transform.rotation,Quaternion.LookRotation(target.position-transform.position),300*Time.deltaTime);
1 Like
transform.LookAt(target, transform.up);

This is what I need. Thank you!!
I needed the axle to not turn over at all, sharply or smoothly.