Custom tool follow mouse?

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.

image
When player equips a tool, a torso Motor6D will connect between character’s Torso and BodyAttach.

Here’s the script that makes it follow the mouse:

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?

This is an expected behavior (From the game Reason 2 Die)

3 Likes

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.

So how do I utilize it? Is this gonna solve the problem of the tool offsetting from the arm?

I feel like I should change how the arm works.

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)

Instead of using CFrame.new(1,0.5,0) , I want the right arm to be attached to the Handle (BodyAttach) of the tool, how?

2 Likes

Sorry for the Necropost, but I have a solution:

Do this:

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)

Rather than this:

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)
4 Likes

I mixed them around, my bad. It’s fixed now.