i know there’s loads of topics on “pointing the arm towards the cursor” but i went thru a lot of topics and i can’t seem to get why my arm is moving position instead of rotating. im trying to rotate the arm instead. i appreciate any help ty!!
I’m no expert at this sort of stuff, but I believe that you are rotating your arm over a much larger axis, which is causing it appear as if the position is changing.
The arm is changing position because you’re directly editing the position of the arm.
If we ignore the CFrame.Angles part of this line, this code basically says, “Hey, RightShoulder, position yourself along the x-axis based on the x-position of the mouse divided by pi squared.” This is why you can see the arm in the video moving in a straight line from left to right whenever you move the mouse.
Here are your first two steps:
Change C0 to Transform
This is internally what Roblox uses to render animations. By default, if no animation is playing, Transform is equal to an empty CFrame (which is just CFrame.new(0, 0, 0)).
Change RenderStepped to PreSimulation
If you don’t do this, the Transform property of RightShoulder will get overridden by the animations that are being played by the character. Running code during PreSimulation lets us manipulate the joints of the character after they get animated instead of before.
Here is the revised version of your code (with more comments than code):
-- Creating a variable because we specifically need the initial C0 of the shoulder for our calculations.
-- C0 is the offset of the shoulder from the HumanoidRootPart (aka the CFrame of
-- RightShoulder in the "object space" of HumanoidRootPart).
local initialC0 = bpTable.RightShoulder.C0
connection = rs.PreSimulation:Connect(function()
local char = p.Character
-- Find the CFrame of the shoulder/Motor6D in world space (workspace coordinates).
local jointCFrame = char.HumanoidRootPart.CFrame:ToWorldSpace(initialC0)
-- Make a CFrame that has the same position as the shoulder/Motor6D, but also make it look at the mouse.
-- This is what we want the CFrame of the shoulder to be, but this is in world space while
-- the CFrames used in Motor6Ds are in object space, so we convert that next.
local desiredcf = CFrame.lookAt(jointCFrame.Position, mo.Hit.Position)
-- Convert 'desiredcf' into the object space of the shoulder.
-- This offset is how much we should rotate the Motor6D so that it matches
-- 'desiredcf' regardless of which way the character is facing.
-- If we use 'desiredcf' directly, then it would only work correctly
-- if the character is facing a certain direction.
local offsetFromJointToDesired = jointCFrame:ToObjectSpace(desiredcf)
local x, y, z = offsetFromJointToDesired:ToEulerAnglesXYZ() -- the rotation of this CFrame
local position = CFrame.new(0, 0, 0) -- we don't want to move the shoulder, so this is an empty CFrame
local rotation = CFrame.Angles(x, y, z) -- rotate the Motor6D so that it matches 'desiredcf'
local offset = CFrame.Angles(math.rad(90), 0, 0) -- rotate the arm 90 degrees so that it points to the mouse instead of down
bpTable.RightShoulder.Transform = position * rotation * offset
-- Straighten the elbow so that the arm is straight.
-- Delete this line if you don't want a straight elbow.
char.RightLowerArm.RightElbow.Transform = CFrame.new(0, 0, 0)
end)
Side note (not important)
local jointCFrame = char.HumanoidRootPart.CFrame:ToWorldSpace(initialC0)
is the same as
local jointCFrame = char.HumanoidRootPart.CFrame * initialC0
Likewise this:
local offsetFromJointToDesired = jointCFrame:ToObjectSpace(desiredcf)
is the same as this:
local offsetFromJointToDesired = jointCFrame:Inverse * desiredcf
thank u so much for helping me mate, no words shows how grateful i am that u typed up all that with a clear, thorough and a valuable explanation that you gave. there’s a reason you stated for every line of code. that really helps me to understand it a lot more, thank you so much!!
ive spent some time studying your code and methods such as :toWorldSpace and toObjectSpace and i didn’t know about the transform property until now. i’m going to read about the PreSimulation event now. have a great day man!!!
hey man,
sorry for the late reply. it was because i was directly telling the code to move the arm along the x axis along the x axis of the mouse; i didn’t know how to rotate it really until now, but thank you for the help anyways!!