Understanding Motor6D's and Positioning Arms

im trying to make my character arms reposition and rotate in such a way that their end(hands) will be at a certain point in space.

for a more clear example. have my character arms “Hold” the gun by having its arms (left and right) end up repositioning themselves in such a way that, left arm ends up at underbarrel and right arm at the handle/grip (i have parts called Left/Right in the gun tool, the hands should end up there)

im using motor6d’s to try achieve and not animation because i want to make it procedurally. and i have no idea how motor6d’s work. after alot of experimenting i came up with the code below and i gave up.
(what im trying to achieve sounds like Inverse Kinematics?)

the gun is a tool without handle so welds wont be made since im positioning it manually.

the main question i want to ask is procedurally placing a hand at a certain point in space
R6 character model


function SetupGun(plr,gun)
	local char = plr.Character
	local LeftArm = char:FindFirstChild("Left Arm")
	local RightArm = char:FindFirstChild("Right Arm")
	--Position Gun in a spot
	local joint = Instance.new("Motor6D")
	joint.Name = "GunJoint"
	joint.C0 = CFrame.lookAt(Vector3.new(1, -0.5, -1), char.PrimaryPart.CFrame.LookVector);
	joint.Part0 = char.Torso
	joint.Part1 = gun.PrimaryPart 
	joint.Parent = char.Torso
	
	
	local LeftShoulder =  char:FindFirstChild("Left Shoulder",true)
	local RightShoulder =  char:FindFirstChild("Right Shoulder", true)
	local Right = gun:FindFirstChild("Right",true)
	local Left = gun:FindFirstChild("Left",true)
	
	LeftShoulder.C0 = char.Torso.CFrame:ToObjectSpace(Left.CFrame)
	RightShoulder.C0 = char.Torso.CFrame:ToObjectSpace(Right.CFrame)
	
end

What you describe is perfectly achievable by inverse kinematics, which you name-drop in your post. Have you tried using it at all yet? If not, take a look:


This is an example from one of my projects, using this resource as a starting point, which you may have seen, but you can find a plethora of posts within the forums using the exact same method described there for both arms and legs.

Edit: Regarding your code itself, if you strictly want the arm to point toward a target, then you should take a look at CFrame.lookAt. You’ll need both a position from your Motor6D (i.e. a stored C0 CFrame position) and a position from your target, ideally translated into your Motor6D’s Part0 object space. From there, you’ll have to play around with angles and offsets.

5 Likes

Thank you for replying.
At first i thought of learning IK to achieve what i wanted becuase, its the perfect application.

just got drifted away by how others did it. Thanks for providing the learning resource and an example as well as a solution with CFrame.lookAt()(idk why i didnt use this).

edit: i do have very little knowledge on IK, may i ask how i can integrate normal animations into it? im guessing i just turn off the calculations during the animation and thats it?

1 Like

If you still want to animate the arms manually, then yes, I would disable the IK solver while an animation is playing. Otherwise, you’ll have both the IK solver and animation moving the arms toward their own goals at the same time in an odd, overlapping look.

Do keep in mind that having both the IK solver and an animation working together is useful in other scenarios. A popular example is foot planting (not to be confused with procedural walk cycles, but often related) in videogames, which is typically done like that, and is also another topic that you can find plenty of information for online.

1 Like