I’m working on a “custom tool” and I want it to follow the mouse cursor.
It still uses the Roblox Tool but I do not use the ToolGrip Weld, instead I use a Motor6D.
In my tool, there’s a part that holds all the other parts, named BodyAttach. It holds other part by using Motor6Ds.
When player equips a tool, a torso Motor6D will connect between character’s Torso and BodyAttach.
game:GetService("RunService").RenderStepped:Connect(function()
if char.Humanoid.Health ~= 0 then
if char:FindFirstChildOfClass("Tool") then
char.Torso["Right Shoulder"].C0 = CFrame.new(1,0.5,0) * CFrame.Angles(-math.asin((mouse.Origin.p - mouse.Hit.p).unit.y),1.55,0)
char.Torso["Left Shoulder"].C0 = CFrame.new(-1,0.5,0) * CFrame.Angles(-math.asin((mouse.Origin.p - mouse.Hit.p).unit.y),-1.55,0)
char.Torso:WaitForChild("ToolGrip").C0 = CFrame.new(0,0,0) * CFrame.Angles(-math.asin((mouse.Origin.p - mouse.Hit.p).unit.y),0,0)
else
char.Torso["Right Shoulder"].C0 = RS
char.Torso["Left Shoulder"].C0 = LS
end
end
end)
However this makes the tool goes off the arm when it was being pointed too high (Notice the shooting handle and the Right Arm), is there any ways to improve the code or anything wrong such that this doesn’t happen? https://gyazo.com/d8733aa81a553575385d51556c548c21
I don’t think R2DA uses the PlayerMouse object to determine the direction that the player is pointing, rather makes use of ViewportPointToRay for it. I think this would be more accurate since it casts a unit ray from the screen to a point.
Not sure about this one truthfully. Posting down what I think may be the case.
local RightShoulder, LeftShoulder, ToolGrip = char.Torso["Right Shoulder"], char.Torso["Left Shoulder"], char.Torso:WaitForChild("ToolGrip")
local X = -(math.asin((mouse.Origin.p - mouse.Hit.p).unit.y))
local _, Y, Z = RightShoulder.C0:ToEulerAnglesXYZ()
local OldRightC0 = RightShoulder.C0
RightShoulder.C0 = CFrame.new(RightShoulder.C0.Position) * CFrame.Angles(X, Y, Z)
local Difference = OldRightC0.Position - RightShoulder.C0.Position
ToolGrip.C0 = (ToolGrip.C0 + CFrame.new(Difference)) * CFrame.Angles(X, Y, Z)
local _, Y, Z = LeftShoulder.C0:ToEulerAnglesXYZ()
LeftShoulder.C0 = CFrame.new(LeftShoulder.C0.Position) * CFrame.Angles(X, Y, Z)