How can I make the players arm point towards their mouse on one axis?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want players to be able to point their characters arm towards their mouse
  2. What is the issue? Include screenshots / videos if possible!
    I have found a way to do this but I only want their arm to rotate like this, which I can’t figure out. I don’t want them to be able to rotate their arm forwards or backwards, only in this motion. I also only want their right arm to move. I also want the motion to be limited to only go up to 180 degrees.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have tried a script that points the players arm to their mouse but it is on all axis
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

local plr = Players.LocalPlayer
local char = plr.Character
local mouse = plr:GetMouse()

local armOffset = char.UpperTorso.CFrame:Inverse() * char.RightUpperArm.CFrame

local armWeld = Instance.new("Weld")
armWeld.Part0 = char.UpperTorso
armWeld.Part1 = char.RightUpperArm
armWeld.Parent = char

RunService.Heartbeat:Connect(function()
	local cframe = CFrame.new(char.UpperTorso.Position, mouse.Hit.Position) * CFrame.Angles(math.pi/2, 0, 0)
	armWeld.C0 = armOffset * char.UpperTorso.CFrame:toObjectSpace(cframe)
end)
1 Like

I also do not want it to be able to rotate in circles it should always be facing forward

I can help you with this but I can’t tell what that picture is depicting.

You mean like this?

image

Or this?

image

The first one where it only goes upwards sideways.

Sorry for the unformatted response, I’m on mobile.

local lookVector = (HumanoidRootPart.Position - hit.Position).Unit
weld.C0 = CFrame.new() * CFrame.Angles(math.atan2(lookVector.Y,lookVector.X),0,math.pi/2)

Edit: It would be much easier for you to use the built-in arm Motor6D than create your own weld.

I implemented this code and it looks like this, the arm sticks inside of the hrp and only rotates in circles not up and down.

image

This seems to do what you wanted.

local player = game.Players.LocalPlayer
local char = player.Character
local uis = game:GetService("UserInputService")
local rs = game:GetService("RunService")
local rightShoulder: (Motor6D) = player.Character:FindFirstChild("RightShoulder", true)
local originalC1 = rightShoulder.C1

function getMouseDirection(): (Vector3)
	local mouseCoordinates: (Vector2) = uis:GetMouseLocation()
	local ray: (Ray) = workspace.CurrentCamera:ScreenPointToRay(mouseCoordinates.X, mouseCoordinates.Y, 300)
	
	return ray.Direction
end


rs:BindToRenderStep("pointRightArmAtMouse", 201, function()
	local maxAngle: (number) = math.rad(20)
	local mouseDirection: (Vector3) = getMouseDirection().unit
	local angleBetweenMouseVector = math.atan2(mouseDirection.Y, 1)
	local intensityAdjustment: (number) = 2
	
	-- Clamp the angle and multiply it against the intensity to better show movement
	angleBetweenMouseVector = math.clamp(angleBetweenMouseVector, 0, maxAngle) * intensityAdjustment
	
	-- Set the C1 of the right shoulder
	rightShoulder.C1 = originalC1 * CFrame.fromAxisAngle(Vector3.new(0,0,-1), angleBetweenMouseVector)
end)

thank you so much but it seems to only go up if my mouse is over half of the screen is there anyway to make it always follow my mouse?

No, it can’t “follow your mouse” because it has to be relative to something and this is a direction going the opposite than the mouse is pointing. It’s rotating sideways - the mouse is facing forwards.

You want it to be relative to the screen instead of the world? As in, where the mouse is on the screen?

I want it to work like this but this is inverted. I want the players arm to always be pointing to where the player’s mouse is on their screen.

https://streamable.com/re1px1

Yeah that’s a completely separate thing, but this should work. You can adjust the maximum height on the maxAngle variable.

local player = game.Players.LocalPlayer
local char = player.Character
local uis = game:GetService("UserInputService")
local rs = game:GetService("RunService")
local rightShoulder: (Motor6D) = player.Character:FindFirstChild("RightShoulder", true)
local originalC1 = rightShoulder.C1

rs:BindToRenderStep("pointRightArmAtMouse", 201, function()
	local maxAngle: (number) = math.rad(135)
	local screenSize: (Vector2) = workspace.CurrentCamera.ViewportSize
	local mouseCoordinates: (Vector2) = uis:GetMouseLocation()
	local centerCoordinates: (Vector2) = screenSize/2
	local difference: (Vector2) = mouseCoordinates - centerCoordinates
	local yCoordinateFromCenter: (number) = difference.Y
	local angle: (number) = -math.atan2(yCoordinateFromCenter, screenSize.Y/2)
	angle += math.pi/4
	local percentage: (number) = angle / (math.pi/2)
	local finalAngle: (number) = (maxAngle) * percentage
	---- Set the C1 of the right shoulder
	rightShoulder.C1 = originalC1 * CFrame.fromAxisAngle(Vector3.new(0,0,-1), math.clamp(finalAngle, 0, maxAngle))
end)
2 Likes