Issue with CFrames and body alignment

Hello there, fellow scripters, I’ve been trying to get this CFrame conversion to work out but i keep getting the same problem. The tank’s cannon is supposed to face the mouse direction whenever it shoots.

Here is an example of the sytem working correctly. When the body is aligned with the Z axis, it changes the C0 of the weld bellow the cannon, making it face the right direction.

But when the body rotates to another alignment (since it’s mobile) it wont work properly.

This is the code I’m currently using to get the cannon to face the right direction based on the mouse position:

local offset = rotWeld.C0 -- called before the function, so I can get the default C0.

-- Function begins --
local r = -info.plrRot -- plrRot: vector3 orientation of the HumanoidRootPart which is received by the server from a remote event that gets it locally.
local desiredCF = offset * CFrame.lookAt(plrPos, info.shootPosition).Rotation * CFrame.fromOrientation(math.rad(r.X),math.rad(r.Y),math.rad(r.Z))

game.TweenService:Create(weld,, {C0 = desiredCF}):Play()

What I tried to do here was adding the player rotation into the equation, but it seems that I’m doing something very wrong here. I’m not very proficient at CFrames, so if you can help me i’d be grateful.

One method is to use the formula for C0 in my tutorial to translate CFrame.lookAt to C0.

I also used the formula in my modulescript for turrets:

1 Like

Woah, you really opened my mind for CFrame concepts, such as the inequality between operations with different sequences of factors. It was what causing the calculation to go wrong, so i just had to reverse it:

local desiredCF = offset * CFrame.fromOrientation(math.rad(r.X),math.rad(r.Y),math.rad(r.Z)) * CFrame.lookAt(plrPos, info.shootPosition).Rotation

now it works properly. Thanks!

1 Like

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