How can I freely rotate character body parts with a script?

Hi! :smiley:
My question is: How can I freely rotate character body parts with a script?
I am not talking about animations right now. In my case, I need the character’s arms to point at mouse cursor, for example. Any help on how I can do that?
Thanks in advance,
Muuuuun

P.S I have asked this everywhere, I never got an answer :frowning:

2 Likes

Hi there,

This is relatively straightforward - there are two ways of doing this.

Do you want the character to point towards the mouse? if so, a BodyGyro is your best bet (in the HumanoidRootPart)

Unfortunately, I believe what you’re specifically asking for is actually quite difficult to achieve (at least if you don’t have a strong understanding of CFrame manipulation)

I’d recommend looking at this model:

https://www.roblox.com/library/187857559/Pistol

and in particular, this function:


local function frame(mousePosition)
	-- Special mobile consideration. We don't want to track if the user was touching a ui
	-- element such as the movement controls. Just return out of function if so to make sure
	-- character doesn't track
	if not mobileShouldTrack then return end
	
	-- Make sure character isn't swiming. If the character is swimming the following code will
	-- not work well; the character will not swim correctly. Besides, who shoots underwater?
	if player.Character.Humanoid:GetState() ~= Enum.HumanoidStateType.Swimming then
		local torso = player.Character.Torso
		local head = player.Character.Head
		
		local toMouse = (mousePosition - head.Position).unit
		local angle = math.acos(toMouse:Dot(Vector3.new(0,1,0)))
		
		local neckAngle = angle
	
		-- Limit how much the head can tilt down. Too far and the head looks unnatural
		if math.deg(neckAngle) > 110 then
			neckAngle = math.rad(110)
		end
		neck.C0 = CFrame.new(0,1,0) * CFrame.Angles(math.pi - neckAngle,math.pi,0)
		
		-- Calculate horizontal rotation
		local arm = player.Character:FindFirstChild("Right Arm")
		local fromArmPos = torso.Position + torso.CFrame:vectorToWorldSpace(Vector3.new(
			torso.Size.X/2 + arm.Size.X/2, torso.Size.Y/2 - arm.Size.Z/2, 0))
		local toMouseArm = ((mousePosition - fromArmPos) * Vector3.new(1,0,1)).unit
		local look = (torso.CFrame.lookVector * Vector3.new(1,0,1)).unit
		local lateralAngle = math.acos(toMouseArm:Dot(look))		
		
		-- Check for rogue math
		if tostring(lateralAngle) == "-1.#IND" then
			lateralAngle = 0
		end		
		
		-- Handle case where character is sitting down
		if player.Character.Humanoid:GetState() == Enum.HumanoidStateType.Seated then			
			
			local cross = torso.CFrame.lookVector:Cross(toMouseArm)
			if lateralAngle > math.pi/2 then
				lateralAngle = math.pi/2
			end
			if cross.Y < 0 then
				lateralAngle = -lateralAngle
			end
		end	
		
		-- Turn shoulder to point to mouse
		shoulder.C0 = CFrame.new(1,0.5,0) * CFrame.Angles(math.pi/2 - angle,math.pi/2 + lateralAngle,0)	
		
		-- If not sitting then aim torso laterally towards mouse
		if not amISitting(player.Character) then
			torso.CFrame = CFrame.new(torso.Position, torso.Position + (Vector3.new(
				mousePosition.X, torso.Position.Y, mousePosition.Z)-torso.Position).unit)
		else
			--print("sitting")		
		end	
	end
end

6 Likes

Thanks for the answer! :D
I’d like to ask, how specifically do you rotate the torso?
Which line does just the final torso rotation?

2 Likes

You’d rotate with:

BodyGyro.CFrame = CFrame.new(Torso.Position, Mouse.Hit.p)
2 Likes

Are you sure?
I am trying just to have the torso (or some other body part) face something, but if I use your method the whole character rotates

2 Likes

I have provided you with the code to facilitate this - you can modify it from the Gun if you wish to remove the torso rotation

3 Likes

If I’m not mistaken, theres a joint that connects to the humanoidrootpart from the torso/lowertorso or any other body part. CFrame/rotate the joint to however you want though you’ll have to locate it into the character and change Part0/Part1.

3 Likes

That’s exactly the part I don’t understand. How do I rotate joints using scripts?

2 Likes

What’s your use case for this? What specifically do you need to rotate?

3 Likes

I need to make the torso or arms point at a certain point, specifically the mouse cursor

1 Like

That’s not a specific use case. That’s what you want to do. What are you doing it for? A view model for a tool? A way to orient arms to a steering wheel? What is your use case?

3 Likes

I am trying to make a gun-type tool thing yeah.

2 Likes