i have a script that makes your arm follow the cursor (credits to the guy that made it). 2 problems. 1. my arm goes backwards when im moving my mouse and 2. when i equip a tool my arm like spasms out. i have no idea what im doing… ty!
script:
local plr = game.Players.LocalPlayer
local char = script.Parent
local mouse = plr:GetMouse()
local leftarm = char["Left Arm"]
local rightarm:Part = char["Right Arm"]
local rs = game:GetService("RunService")
local Torso = char.Torso
local leftshoulder = Torso["Left Shoulder"]
local rightshoulder:Motor6D = Torso["Right Shoulder"]
local clamp = math.clamp(rightshoulder.C0.X, -1, 1)
local cframeAng = CFrame.Angles(0, 0, clamp)
local function Point_C0_To_Mouse(Motor6D, WorldCFrame)
local Part1_CFrame = Motor6D.Part1.CFrame
local Stored_C1 = Motor6D.C1
local Stored_C0 = Motor6D.C0
local RelativeTo_Part1 = Stored_C0 * Stored_C1:Inverse() * Part1_CFrame:Inverse() * WorldCFrame * Stored_C1
RelativeTo_Part1 -= RelativeTo_Part1.Position
local Goal_C0 = RelativeTo_Part1 + Stored_C0.Position
return Goal_C0
end
rs.Stepped:Connect(function()
local zAxisLimit = math.clamp(90, -90, 90)
local Mouse_Pos = mouse.Hit.Position
rightshoulder.C0 = Point_C0_To_Mouse(rightshoulder, CFrame.lookAt(rightarm.Position, Mouse_Pos)) * cframeAng
end)
I have worked on this for some time now, and I can say that the arm going backwards is simply due to how the lookat cframe object works. It may be possible to fix it with some more intelligent manipulation of how the arm should point towards the object, possibly with look vector, I encourage you to look into it and figure out how the cframes work on a deeper level.
The arm spasms are most likely due to the animation that It plays when holding a tool, I think this may be the case because, when walking, I saw the arm shifting around according to the animation.
I made my own version of the arm point script for use on a static model and a part on the ground which I used to test this.
local RS=game:GetService("RunService")
local Rig=game.Workspace.Rig
local Pointer=game.Workspace.PointerPart
local RightArm=Rig["Right Arm"]
local RightShoulder=Rig.Torso["Right Shoulder"]
RS.Heartbeat:Connect(function()
local ArmOffset=(RightArm.CFrame*CFrame.new(0.5,0.5,0)).Position --Center of arm pivot
local Angle=CFrame.lookAt(ArmOffset,Pointer.Position).Rotation --Gets the angle to point from the arm pivot to the part
local ShoulderPosition=RightShoulder.C0*RightShoulder.C0.Rotation:Inverse() --Gets the position of the shoulder with no rotation
local ShoulderOffset=CFrame.Angles(0,math.rad(90),math.rad(90)) --Angles the arm so the correct face of the arm is pointing towards the part
local FinalShoulder=ShoulderPosition*Angle*ShoulderOffset --Combines it all together
RightShoulder.C0=FinalShoulder --Sets the angle and position
end)
I also found that if the part is directly under the arm, the arm starts twirling around, This is most likely due to the lookat not preferring any specific angle for the other angles as no change to them changes where the arm is pointing.
This is a very complicated subject, and I commend you for working on this. Happy Coding!
EDIT: Apparently rotating the rig using my code above rotates the arm with it, when you would expect the arm to keep pointing at the part. I dont know why this is happening.
hey guys, tysm 4 typing all that up. i appreciate the help. its definitely a step closer to solving it now so ty. ur solution is really good and u explained it well.
you lot wouldn’t have a few good resources for learning CFrames more in depth would u?