Why is my follow spotlight doing this?

My follow spotlight module which trigonometry is used for the logic works perfectly.

The one issue is that, I tested with two spotlight fixtures and one of them have an issue like when I’m walking to the other side of the position 0, 0, 0, the fixture’s head does a whole turn around to continue instead of following me there and going along as I go.

Here’s a video of the issue:

Can anyone help how I can fix this?