Cannon behaves weirdly when pointing up

Hello! I have a cannon, attached to a moving truck, which I am trying to point to my mouse.

To do this, I have 2 welds, the first one to control the yaw, and the second one to control the pitch. The first weld is connected to the base, and second connected to the barrel.

To turn the 2 welds c0’s to my mouse I use cframe lookat. But to make sure I only change the yaw/pitch, I do this piece of code:

local RootPos, MousePos = yawbodyweld.Position, mouse.Hit.Position

yawC0 =,, RootPos.Y, MousePos.Z))
pitchC0 =,, MousePos.Y, MousePos.Z))	

The yaw works good, no issues. But the pitch does not work. It behaves very weirdly, especially if pointed up or to the sides.

Heres an video of the issue:

I’ve been stuck for so long any help is appreciated!!!

You’re effectively multiplying two directions which doesn’t do what you think it does unless one of them is close to identity (no rotation at all), which is why it appears to work when pointing forward. You should separately calculate the pitch and yaw rotation. Right now you are calculating a combined rotation and using it twice. You can calculate just a yaw by removing the Y component before doing the lookAt. You can calculate the pitch by replacing the Z component with the length of the X and Y components by themselves. You could also use trigonometry to do each of these but I hate trig because it tempts people to use inverse trig which is almost always used wrong.

1 Like

Thank you much! Should work!!!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.