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

How do I weld a part's position but not its rotation?

Asked by 7 years ago

Howdy. I am welding a gun to my character's right hand. It cannot be a tool for many reasons, first and foremost that it needs to rotate to face my mouse (think Metroid Prime). This is the code inside my gun:

wait(2)
playerTable = game.Players:GetChildren()
player = playerTable[1]
print(player)
mouse = player:GetMouse()
weld = Instance.new("Weld")
weld.Parent = script.Parent
weld.Part0 = weld.Parent
weld.Part1 = player.Character["Right Arm"]
weld.C0 = CFrame.new(0,1,0)
while true do
    wait()
    script.Parent.CFrame = CFrame.new(script.Parent.CFrame.p, mouse.Hit.p)
end

As you would expect, this code welds the gun to the player's right arm one stud up. The problem comes with the CFrame's second vector. It works perfectly, sort of: the gun does rotate towards the mouse. The problem? It is welded to my character, so my character faces my mouse as well. Any help is appreciated. Thanks in advance.

1 answer

Log in to vote
0
Answered by
kazeks123 195
7 years ago

In your place I would use Constraints rather than welding, because they allow for movement / rotation: http://wiki.roblox.com/index.php?title=Constraints . Seems that Ball in Socket Constraint would do for what you're trying to do: http://wiki.roblox.com/index.php?title=API:Class/BallSocketConstraint . It will allow both parts to rotate freely, so you'll just have to adjust gun's rotation.

0
That sounds promising. I'll look into that UnityAlex 36 — 7y
0
That worked phenomenally! Thank you! UnityAlex 36 — 7y
Ad

Answer this question