Making an arm of an R6 character face a part using Motor6Ds

Making an arm of an R6 character face a part using Motor6Ds? I know it’s possible but I just don’t know the math for it. Basically, am trying to make a punch script, where if you hold down a key it makes your right shoulder Motor6D follow your mouse, yeah, and if you click, then it launches the fist. Pretty cool idea, I know, but I don’t know the math for it.
The basic hypothetical idea is

workspace.Dummy.Torso["Right Shoulder"].C1 = workspace.Dummy.Torso["Left Shoulder"].C1*CFrame.fromEulerAnglesXYZ(math.rad(RX),math.rad(RY),math.rad(RZ))

Now the problem is, I don’t know what to make RX and RY and RZ. So if somebody could please help me with this and provide a thorough and correct explanation that’d be great.
Thanks! :smiley:

1 Like

If you are still stuck try doing this ?

local dummy = workspace.Dummy
local targetpart = workspace.Target

local Torso = dummy:WaitForChild("Torso")
local RightShoulder = Torso:FindFirstChild("Right Shoulder")
local shoulderSize = RightShoulder.Part1.Size
local fixedRS = RightShoulder.Part1.CFrame + Vector3.new(0,shoulderSize.Y/2,0)

local cOffset = Torso.CFrame:ToObjectSpace(fixedRS)

local RunService = game:GetService("RunService")
local forwardV3 = Vector3.new(0,0,-1)

while true do
	local cf = Torso.CFrame * cOffset
	local targetPos = targetpart.Position 

	local planeCf = CFrame.lookAt(fixedRS.Position,targetPos)

	local look = RightShoulder.Part1.CFrame.LookVector
	local relative = RightShoulder.Part1.Position - targetPos
	local dot = look:Dot(relative.unit)

	local cAngle = CFrame.Angles(math.acos(dot),0,0)
	RightShoulder.C0 = Torso.CFrame:ToObjectSpace(planeCf) * cAngle

	RunService.Heartbeat:Wait()
end
2 Likes

Care to explain this code? Also, I want to modify the C1, not the C0

2 Likes

I got a code from this post Making a player's head face a part using a Motor6D - #4 by SatrapRezaPahlavi

--Initialization
local torso = script.Parent:WaitForChild("Torso") --get reference to Torso, change this as needed
local targetPos = Vector3.new(0,0,0) --Change this as needed
local OrigC1 = torso["Right Shoulder"].C1 --store the original C1 value for transformation

--Runtime

game:GetService("RunService").Stepped:Connect(function()
	targetPos=workspace.Target.Position
	local torsoF = torso.CFrame.LookVector--Get the directional vector which the torso is facing
	local torsoR = torso.CFrame.RightVector--Get the directional vector that extends right of the torso
	local actualVect = targetPos - torso.CFrame.Position---Get the directional vector from torso to target

	local rot = math.atan2(torsoR:Dot(actualVect),torsoF:Dot(actualVect))
	--Dot determines how much the target vect faces the torso vectors
	--By doing this, we can construct "relative" coordinates as opposed to "absolute" coordinates.
	--We can then determine the relative angle from these coordinates using atan2
	torso["Right Shoulder"].C1 = CFrame.Angles(rot,rot,rot) * OrigC1
	--Sets the neck every step
end)

But it doesn’t seem to work

Okay, so I made another lackluster equation It works, and then it doesnt work

while true do wait() workspace.Dummy.Torso["Right Shoulder"].C1 = workspace.Dummy.Torso["Left Shoulder"].C1*CFrame.fromEulerAnglesXYZ(math.rad(-workspace["Right Arm"].Orientation.Y),math.rad(workspace["Right Arm"].Orientation.Z),math.rad(workspace["Right Arm"].Orientation.X))
 end

Any help here, friends?

The code that @Razor_IB gave you is correct and will work if you had just tried it.
All his code does it set it up so that the arm is on the same plane as the target position, and then proceeds to find the angle between the arm lookvector and the targetposition once they are on the same plane in order to rotate it such that it is facing the point.

As for changing the c1, there is no need as using c0 will achieve the effect we want, which is making it so the right arm points to the target position.

1 Like

I did try it, for two hours. The shoulder joint offset was incorrect and the amount of variables was just unnecessary. Also, I need to set c1, because c0 is the base CFrame offset, and c1 is the transform.

1 Like

Also, he didn’t explain anything about his code, he just pasted it and expected me to instantly know the ins and outs of it

1 Like

I went out of the way to create a place file with his code in it to show you it working.
And if you really care so much to modify the c1 instead, you can just change some of the math around and it will work as expected.

PointingArm.rbxl (30.5 KB)

To test it, click run and move around the red part, you will see that the Dummy’s right arm points to the targetpart as expected. If you wish to clamp the arm so that it doesnt point behind the character, then you can uncomment the code I put in which will attempt to clamp the angle of the plane cframe.

But incase you don’t care to download the place file, here is also a gif of it working ( with the angle clamping )

https://gyazo.com/4c7b7d0ebede7f067d723da8d09fcc34

Also there is nothing wrong with a lot of variables, especially since it serves as better documentation and can break up long equations or algorithms in to more bite sized pieces.

1 Like

I already tested the script the guy sent me. And how do you expect me to know how to change the math around it? That’s literally the point of this post, to know what math is necessary in order to change C1 such that it makes the arm point to the mouse.

1 Like

Did you not read any of my post, i’m done helping you now, as you seem ignorant to any help given to you. The working code is in the post above, use it at your discretion, or not…

1 Like

I’ll link you to this post which explains clearly the process involved in making it work.

Also both c0 and c1 are offsets, its just that one is the offset from part1 to the motor and the other is the offset from part0 to the motor.

In this case, since we want to change the CFrame of part1 ( since part1 in the motor6d of an r6 character which controls the right arm is the right arm ) we thus need to change the c0 offset.

1 Like

Also it seems as though this isn’t your first topic about this problem… :roll_eyes:

https://devforum.roblox.com/t/make-motor6d-face-a-part/1346247/23

Its funny that in all your posts you seem to get really defensive and aggressive towards people when they are just trying to help.
You should just chill a bit man, maybe touch some grass? I don’t know, whatever it takes to make you calm down over a simple problem for a roblox game.

:neutral_face:

5 Likes

Its because you have set the fixedRS inside the scope of the connection

It’s wrong

local mou = game.Players.LocalPlayer:GetMouse()
local f = false
local og = script.Parent.Torso["Right Shoulder"].C0
mou.KeyDown:Connect(function(k)
	if string.lower(k)=="f" then
		f = true
	end
end)
mou.KeyUp:Connect(function(k)
	if string.lower(k)=="f" then
		f = false
	end
end)
game:GetService("RunService").RenderStepped:Connect(function()
	if f==true then
		script.Parent.Torso["Right Shoulder"].C0=og
		local shoulderSize = script.Parent.Torso["Right Shoulder"].Part1.Size
		local fixedRS = script.Parent.Torso["Right Shoulder"].Part1.CFrame + Vector3.new(0,shoulderSize.Y/2,0)

		local cOffset = script.Parent.Torso.CFrame:ToObjectSpace(fixedRS)
		local cf = script.Parent.Torso.CFrame * cOffset

		local planeCf = CFrame.lookAt(fixedRS.Position,mou.Hit.p)

		local look = script.Parent.Torso["Right Shoulder"].Part1.CFrame.LookVector
		local relative = script.Parent.Torso["Right Shoulder"].Part1.Position - mou.Hit.p
		local dot = look:Dot(relative.unit)

		local cAngle = CFrame.Angles(math.acos(dot),0,0)
		script.Parent.Torso["Right Shoulder"].C0 = script.Parent.Torso.CFrame:ToObjectSpace(planeCf) * cAngle
	else
		script.Parent.Torso["Right Shoulder"].C0=og
	end
end)
1 Like

What’s wrong about it, care to elaborate more. You are are so worthless when it comes to communicating.

1 Like

If you can’t seem to point out anything wrong with the video, copy the script I sent, put it in startercharacterscripts, and you’ll see exactly what I’m talking about

1 Like

[ Sorry for late response, was in the middle of a WarThunder match with a friend ]

Hey thanks for pointing out a typo, you really got me there hahahahaha…

As for why it looks detached, its because the arm is being rotated in un-natural positions, and since this is r6, there is no upper right arm to compensate for the un-natural poses and make it looks slighly more realistic.

Though here i’ve attempted to make it slighly better, by clamping the angle so that it cant look behind the character, and using a new method to get the plane cframe, which might fix issues where the arm angle isn’t correct.

The correct approach to here to have no weird detachment, or un-natural bending, I believe would be a 2 joint system, with inverse kinematics to set the joints.

Here is my updated code for you

local mou = game.Players.LocalPlayer:GetMouse()
local f = false
local og = script.Parent.Torso["Right Shoulder"].C0

local shoulderSize = script.Parent.Torso["Right Shoulder"].Part1.Size
local fixedRS = script.Parent.Torso["Right Shoulder"].Part1.CFrame + Vector3.new(0,shoulderSize.Y/2,0)

local cOffset = script.Parent.Torso.CFrame:ToObjectSpace(fixedRS)
local forwardV3 = Vector3.new(0,0,-1)

game:GetService("RunService").RenderStepped:Connect(function()
		local cf = script.Parent.Torso.CFrame * cOffset
		local hit = mou.Hit.p
	
		local localized = cf:PointToObjectSpace(hit)
		local localizedUnit = localized.Unit

		local axis = forwardV3:Cross(localizedUnit)
		local angle = math.clamp(math.acos(-localizedUnit.Z),-math.pi/2,math.pi/2)
		local planeCf = cf * CFrame.fromAxisAngle(axis,angle)
	
		local look = script.Parent.Torso["Right Shoulder"].Part1.CFrame.LookVector
		local relative = script.Parent.Torso["Right Shoulder"].Part1.Position - hit
		local dot = look:Dot(relative.unit)

		local cAngle = CFrame.Angles(math.acos(dot),0,0)
		script.Parent.Torso["Right Shoulder"].C0 = script.Parent.Torso.CFrame:ToObjectSpace(planeCf) * cAngle
end)

PS: Please don’t start whining because you may have not got exactly what you wanted.
Perhaps someone else, who knows more than me can help you better, though I think we’ve reached the limit of what an r6 model can do naturally. This is my final post here, cheers and please do…
touch some grass

1 Like

Why are you raging at @Razor_IB and @Xx1Luffy1xX when you haven’t even understood what C0 and C1 are correctly?

Maybe if you actually consider what others advise you with you would solve your issues a lot smoother.

Here’s the link to the DevHub page on Joints in which the definitions of C0 and C1 are explained:
https://developer.roblox.com/en-us/api-reference/class/JointInstance

4 Likes