You can write your topic however you want, but you need to answer these questions:
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
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.
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)
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)
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?
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)