Hi! I am trying to attach a turret to a part using welds, however, there’s a problem when changing the C0. Here’s the issue:
See, the turret isn’t where my mouse is pointing at. Here’s the code I am using:
local function heartbeat(delta)
local original = motor.C1:Inverse()
local rotX,rotY,rotZ = original:toOrientation()
local mouse_hit = original:toObjectSpace(mouse.Hit)
local mouse_pos = mouse_hit.p
local rot = v3new(mouse_pos.x,mouse_pos.y,mouse_pos.z)
local aimCF = lookAt(pivot.Position,rot);
local x,y,z = aimCF:toOrientation()
x = (sign(x) == 1 and x > xMax and xMax) or (sign(x) == -1 and x > xMin and xMin) or (x)
y = (sign(y) == 1 and y > maxAngle and maxAngle) or (sign(y) == -1 and y < -maxAngle and -maxAngle) or (y);
local c0 = motor.C0:Inverse()
local p = pivot.Position
aimCF = cf_new(p)*(fromOrientation(x,(y+rotY),0))
motor.C0 = aimCF:Inverse()
end
runService.Heartbeat:Connect(heartbeat)
Any help will be appreciated, thank you!
What are you doing with this to object space?
I just don’t understand what you are trying to do, make mouse position relative to object space? If so then there is no need to because you are using pivot.Position
I believe Pivot.Position is a parts position hence in world space so there is no need to combine it with rot which is mouse position in object space of an object space which is the C1.
Why not just use my mostly working resource which does the job?
My CFrame formula Is just the part0 CFrame inversed times the CFrame.lookAt in world space terms like part.Position and mouse hit.Positiom, rotation only applied to the C0 of the Joint Instance while keeping the original C0 position in order to avoid the joint from moving out of position.
local relativeToWorld = currentJointMotor6D.Part0.CFrame:Inverse()
local lookAtWorld = CFrame.lookAt(turretPosition,lookAtPosition,baseCFrame.UpVector)--goal LookAt CFrame
goalCFrame = relativeToWorld*lookAtWorld
I fixed it. Here’s the final code:
local function heartbeat(delta)
local original = workspace.Part.CFrame;
local mouse_hit = mouse.Hit
local mouse_pos = mouse_hit.p
local rot = v3new(mouse_pos.x,mouse_pos.y,mouse_pos.z)
local aimCF = lookAt(pivot.Position,rot);
aimCF = original:toObjectSpace(aimCF)
local x,y,z = aimCF:toOrientation()
x = (sign(x) == 1 and x > xMax and xMax) or (sign(x) == -1 and x > xMin and xMin) or (x)
y = (sign(y) == 1 and y > maxAngle and maxAngle) or (sign(y) == -1 and y < -maxAngle and -maxAngle) or (y);
local c0 = motor.C0:Inverse()
local p = c0.Position
aimCF = cf_new(p)*(fromOrientation(x,(y+rotY),0))
motor.C0 = aimCF:Inverse()
end
runService.Heartbeat:Connect(heartbeat)
Thank you for replying, I wasn’t even expecting a reply