So I'm experimenting with SetPrimaryPartCFrame(), trying to make some remote controlled gun but having issues.
As far as I know, a CFrame value includes: Position, Orientation. That's one confusing thing and that's what I've done.
I want to make the remote controlled gun stay in it's position but rotate to the camera.
So I'm doing CIWS:SetPrimaryPartCFrame(CIWS:GetPrimaryPartCFrame().p, Camera.CFrame.lookVector)
the entire script that moves the gun is this:
RunService.HeartBeat:connect(function() workspace.CIWS:SetPrimaryPartCFrame(CIWS:GetPrimaryPartCFrame().p, Camera.CFrame.lookVector) end)
But no, I get this: The gun moves a bit from it's start position and it doesn't rotate.
Did i miss something or 'am i doing this completely wrong?
You have to construct a new CFrame
inside of the SetPrimaryPartCFrame()
function instead of providing two separate arguments. In addition, the look vector must be set to the camera's position and not the look vector because you want the gun to be rotated towards the camera's position and not its look vector. So in order to correctly set the CFrame
of the gun you can do this:
RunService.HeartBeat:Connect(function () local lookVector = Camera.CoordinateFrame.p local posVector = workspace.CIWS:GetPrimaryPartCFrame().p local cf = CFrame.new(posVector , lookVector) workspace.CIWS:SetPrimaryPartCFrame(cf) end)