I am currently working on a game where I have a mechanic that has your arms follow your mouse when you have a tool equipped. The tool is not following the arms. This is an issue with Motor6D, because my tool is not using a handle, and is rigged to the Torso, not the arm. How do I make it so that the tool also moves, even though it isn’t rigged to the arm? I’ve already tried looking through the dev forum but I couldn’t find an already-existing post regarding a similar issue.
Here is my code:
local defaultRightArmC0 = character:WaitForChild("Torso"):WaitForChild("Right Shoulder").C0
local defaultLeftArmC0 = character:WaitForChild("Torso"):WaitForChild("Left Shoulder").C0
local mouse = player:GetMouse()
local IsEquipped = false
game:GetService("RunService").RenderStepped:Connect(function()
if IsEquipped == true then
if character.Torso:FindFirstChild("Right Shoulder") then
character.Torso["Right Shoulder"].C0 = character.Torso["Right Shoulder"].C0:Lerp(CFrame.new(1, 0.5, 0) * CFrame.Angles(-math.asin((mouse.Origin.p - mouse.Hit.p).unit.y), 1.55, 0, 0) , 0.1)
end
if character.Torso:FindFirstChild("Left Shoulder") then
character.Torso["Left Shoulder"].C0 = character.Torso["Left Shoulder"].C0:Lerp(CFrame.new(-1, 0.5, 0) * CFrame.Angles(-math.asin((mouse.Origin.p - mouse.Hit.p).unit.y), -1.55, 0, 0) , 0.1)
end
elseif IsEquipped == false then
character.Torso["Right Shoulder"].C0 = character.Torso["Right Shoulder"].C0:Lerp(defaultRightArmC0, 0.1)
character.Torso["Left Shoulder"].C0 = character.Torso["Left Shoulder"].C0:Lerp(defaultLeftArmC0, 0.1)
end
end)
game:GetService("RunService").RenderStepped:Connect(function()
for i,v in pairs(character:GetChildren()) do
if v:IsA("Tool") then
IsEquipped = true
else
IsEquipped = false
end
end
end)
game.ReplicatedStorage.MovementEvent.OnClientEvent:Connect(function(otherPlayer, neckCFrame, rightShoulderCFrame, leftShoulderCFrame)
local RightShoulder = otherPlayer.Character.Torso:FindFirstChild("Right Shoulder", true)
local LeftShoulder = otherPlayer.Character.Torso:FindFirstChild("Left Shoulder", true)
if RightShoulder then
RightShoulder.C0 = rightShoulderCFrame
end
if LeftShoulder then
LeftShoulder.C0 = leftShoulderCFrame
end
end)
game:GetService("RunService").RenderStepped:Connect(function()
game.ReplicatedStorage.MovementEvent:FireServer(character.Torso["Right Shoulder"].C0, character.Torso["Left Shoulder"].C0)
end)
The way that I am handling animations is by having the gun rigged separately from the arm so that when I am animating, the handle will not move corresponding to the arm. That is why it is rigged to the Torso. Also, when I rigged it to the Right Arm, the weapon did move with the arms, but an attachment that is on the weapon which is important did not move along with the gun and arms.
copy-paste the C0 code for the right arm and left arm and make one for the gun motor,
perhaps something like this
if character.Torso:FindFirstChild("Right Shoulder") then
character.Torso["Right Shoulder"].C0 = character.Torso["Right Shoulder"].C0:Lerp(CFrame.new(1, 0.5, 0) * CFrame.Angles(-math.asin((mouse.Origin.p - mouse.Hit.p).unit.y), 1.55, 0, 0) , 0.1)
end
if character.Torso:FindFirstChild("Left Shoulder") then
character.Torso["Left Shoulder"].C0 = character.Torso["Left Shoulder"].C0:Lerp(CFrame.new(-1, 0.5, 0) * CFrame.Angles(-math.asin((mouse.Origin.p - mouse.Hit.p).unit.y), -1.55, 0, 0) , 0.1)
end
if GunWeld then --Find GunWeld yourself, I don't know your gun setup
GunWeld.C0 = GunWeld.C0:Lerp(CFrame.Angles(-math.asin((mouse.Origin.p - mouse.Hit.p).unit.y), 0, 0), 0.1)
--[[
im not sure if this math is correct but considering your gun's C0
is most likely going to be an Identity CFrame it should help
]]--
end
like I said, I was iffy on my cframe math, but it shows you’re on the right track, you just want the pivot of the arms and the pivot of the gun to be in the same position
last try from me, if this doesnt work then you’re gonna need to tinker with the cframe math
if character.Torso:FindFirstChild("Right Shoulder") then
character.Torso["Right Shoulder"].C0 = character.Torso["Right Shoulder"].C0:Lerp(CFrame.new(1, 0.5, 0) * CFrame.Angles(-math.asin((mouse.Origin.p - mouse.Hit.p).unit.y), 1.55, 0, 0) , 0.1)
end
if character.Torso:FindFirstChild("Left Shoulder") then
character.Torso["Left Shoulder"].C0 = character.Torso["Left Shoulder"].C0:Lerp(CFrame.new(-1, 0.5, 0) * CFrame.Angles(-math.asin((mouse.Origin.p - mouse.Hit.p).unit.y), -1.55, 0, 0) , 0.1)
end
if GunWeld then --Find GunWeld yourself, I don't know your gun setup
GunWeld.C0 = GunWeld.C0:Lerp(CFrame.new(0, 0.5, 0) * CFrame.Angles(-math.asin((mouse.Origin.p - mouse.Hit.p).unit.y), 0, 0), 0.1)
--[[
im not sure if this math is correct but considering your gun's C0
is most likely going to be an Identity CFrame it should help
]]--
end
i am 90% sure this is just going to end up shifting your gun too far upwards, if it is too far upwards you can prolly just reconstruct the CFrame with the position shifted downwards
What you said happened, but how do I tweak with the CFrame math for me to shift it downwards? If I change the Y value on the CFrame it still slides again.
the y value is needed for the pivot point to be properly calculated, you just need to subtract 0.5 from the y axis after the rotation. doing CFrame - Vector3 results in it being calculated in world space. Since its still finding the target CFrame for the lerp we need to do it inside the lerp
if character.Torso:FindFirstChild("Right Shoulder") then
character.Torso["Right Shoulder"].C0 = character.Torso["Right Shoulder"].C0:Lerp(CFrame.new(1, 0.5, 0) * CFrame.Angles(-math.asin((mouse.Origin.p - mouse.Hit.p).unit.y), 1.55, 0, 0) , 0.1)
end
if character.Torso:FindFirstChild("Left Shoulder") then
character.Torso["Left Shoulder"].C0 = character.Torso["Left Shoulder"].C0:Lerp(CFrame.new(-1, 0.5, 0) * CFrame.Angles(-math.asin((mouse.Origin.p - mouse.Hit.p).unit.y), -1.55, 0, 0) , 0.1)
end
if GunWeld then --Find GunWeld yourself, I don't know your gun setup
GunWeld.C0 = GunWeld.C0:Lerp(CFrame.new(0, 0.5, 0) * CFrame.Angles(-math.asin((mouse.Origin.p - mouse.Hit.p).unit.y) - Vector3.new(0, 0.5, 0), 0, 0), 0.1)
--[[
when you add and subtract vectors from a CFrame the operations happen
in "world space" which means the rotation of the CFrame is not taken into account
]]--
end
for more in depth CFrame math, Roblox’s documentation is extremely good