Make turret face camera with C0

Im trying to use welds to make turrets on my drone face the cameras direction regardless of its orientation. I have 2 problems, It always goes relative to the drones forward position, and it only rotates on the Y axis instead of all.

Video:

Code:

local plrCam = workspace.CurrentCamera
local direction = plrCam.CFrame
local RJointWeld = RightTurret.JointPart.JointWeld
RJointWeld.C0 = CFrame.Angles(direction:ToEulerAnglesXYZ())
LJointWeld.C0 = CFrame.Angles(direction:ToEulerAnglesXYZ())
2 Likes

Formula to convert world space CFrame like part.CFrame into C0

I’ve never used wields before but I say with 50% certainty that you want plrCam.CFrame.LookVector instead.

To answer your questions:

  1. you have to account for the direction your drone is facing to get the true direction of the turret.
  2. avoid using :ToEulerAnglesXYZ() as much as you can, it tends to not preserve rotation correctly (see Gimbal Lock), try to practice working directly with the CFrame matrices.

For the solution, you’ll need the CFrame of your drone body (red block).
This is code for just the right turret weld.

-- code called ever .RenderStepped or .Stepped or wait()
DroneCFrame = CFRAME
TurretDelta = direction * DroneCFrame:Inverse()
RJointWeld.C0 = TurretDelta - TurretDelta.p

I am assuming that the red block’s LookVector is facing forward. If it doesn’t work, please send a photo of how it looks with the new code.

2 Likes

Alright so this solution seems work best, the only problem is the welds are kinda wonky when it comes to Rotating on the Y axis. It only seems to work properly when the drone is facing a specific direction, do you know how I can fix this?

This also works but only on the client. I also want the movement to be seen on the server, but when its hooked up to a remote event the drones movement also effects the turrets.

There’s a lot of ways to go about this, but try this out building upon @treebee63

local dronePart = RJointWeld.Part0 --Should be drone CFrame,
local directionCFrame = plrCam.CFrame
local dronePartCF = dronePart.CFrame

local relativeToBase = dronePartCF :Inverse()*directionCFrame
local x, y, z = relativeToBase:ToOrientation()
local newRelativeToBase = CFrame.fromOrientation(0, y, z) --keep the y z rotation, remove the look up and down x axis rotaiton (along the right vector) 
--Maybe for a cylinder it would be this?
--local newRelativeToBase = CFrame.fromOrientation(x, 0, z) --Depends on the axises

local newDirection = dronePartCF * newRelativeToBase 

TurretDelta = direction * DroneCFrame:Inverse()
RJointWeld.C0 = TurretDelta - TurretDelta.p

BTW I think the new formula for calculating world space to CFrame is this, if you want to know where these formulas are coming from, they all should be from the weld equation

and it’s usually this formula that’s found on the dev forum scattered around, not even sure it’s correct but the formulas usually come from this.

The above formula by @Rare_tendo works well for R15 and R6 rigs as the C1 has no rotation component, but usually fails in a custom rig when there is one, ex C1 = CFrame.fromOrientation(someValues of x y z).

1 Like