Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do you create a joint with Motor6Ds?

Asked by 5 years ago

I am trying to create a joint between two parts such that the joint's position is in the middle of their CFrames. My issue is that I have one segment of code that will offset the joint and maintain the correct rotations, but the part's positions will be changed when I unachor the parts. And I have another segment of code that will maintain the position of the part's but not their rotation. I can't find a middle ground between the two.

Here is the code that maintains rotation, and also a gif of what I mean: GIF: https://gyazo.com/b8ff8876433638e36816aa599689e5df

  function weld(part0, part1)
    local motor6d = Instance.new("Motor6D")
    motor6d.Part0 = part0
    motor6d.Part1 = part1
    motor6d.Name = part1.Name

    local jointPosition = part0.CFrame:Lerp(part1.CFrame, 0.5)
    local jointOffset = jointPosition
    local offset = jointOffset:toObjectSpace(part0.CFrame)

    motor6d.C1 = offset
    motor6d.C0 = offset:Inverse()
    motor6d.Parent = part0

    createVisual(part0, motor6d.C0)
end

Here is the code that maintains position, and a gif of what I mean: GIF: https://gyazo.com/62dd6d148bcd73e63b3e148ebd0d9096

function weld(part0, part1)
    local motor6d = Instance.new("Motor6D")
    motor6d.Part0 = part0
    motor6d.Part1 = part1
    motor6d.Name = part1.Name

    local jointPosition = CFrame.new((part0.Position + part1.Position) / 2)
    local jointRotation = CFrame.Angles(math.rad(part1.Orientation.X), math.rad(part1.Orientation.Y), math.rad(part1.Orientation.Z))
    local jointOffset = jointPosition * jointRotation

    local offset = jointOffset:toObjectSpace(part0.CFrame)

    motor6d.C1 = offset
    motor6d.C0 = offset:Inverse()
    motor6d.Parent = part0

    createVisual(part0, motor6d.C0)

end

Answer this question