Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I point my right arm (R6) to my mouse?

local tool = script.Parent
local player = game.Players.LocalPlayer
player.CharacterAdded:Wait()
local character = player.Character
local humanoid = character:WaitForChild("Humanoid")
local rightarm = character["Right Arm"]
local activate = humanoid:LoadAnimation(script.Activate)
local equip = humanoid:LoadAnimation(script.Equip)
local unequip = humanoid:LoadAnimation(script.Unequip)
local idle = humanoid:LoadAnimation(script.Idle)
local mouse = player:GetMouse()
local connection1
local connection2

local function equipped ()
	equip:Play()
	wait(0.05)
	idle:Play()
	connection1 = game:GetService("RunService").RenderStepped:Connect(function()
		rightarm.LocalTransparencyModifier = 0
	end)
	connection2 = mouse.Move:Connect(function()
		mouse.TargetFilter = workspace
		rightarm.CFrame = CFrame.new(rightarm.Position,Vector3.new(0,mouse.Hit.p.Y,0))
	end)
end

local function unequipped ()
	idle:Stop()
	unequip:Play()
	connection1:Disconnect()
	connection2:Disconnect()
end

local function activated ()
	activate:Play()
end

tool.Equipped:Connect(equipped)
tool.Unequipped:Connect(unequipped)
tool.Activated:Connect(activated)

I'm trying to point my right arm to the mouse while in first person mode (which basically means in the center of the screen). I don't have a single idea how i will do this. I tried changing the cframe, and it obviously didn't work. (I probably need cframe.angles).

And so far... I have made no progress. I would appreciate anyone who would enlighten me how this works.

1 answer

✓ Accepted answer

I am just going to walk you through a very quick way that I put together. If you need to get a better grasp of CFrames there's an amazing post on scriptinghelpers that explains it very well.

First thing we will do get the CFrame offset of the right arm

local armOffset = Character.Torso.CFrame:Inverse() * Character["Right Arm"].CFrame

Then, we want to create create a new Motor6D, and set its properties, which we will use to manipulate the arm's movement and orientation through

local motor = Instance.new("Motor6D")
motor.Part0 = Character.Torso
motor.Part1 = Character["Right Arm"]
motor.Parent = Character["Right Arm"]

We are then going to create a new CFrame starting at the Torso and looking towards the mouse, this is going to be our target CFrame

local modification = CFrame.Angles(math.pi/2, 0, 0)
local cframe = CFrame.new(Character.Torso.Position, Mouse.Hit.Position) * modification

Now to get the position and orientation in space of our target cframe relative to our Torso and set the motor6D's orientation we do:

motor.C0 = armOffset * Character.Torso.CFrame:toObjectSpace(cframe)

All together, I would put this in a LocalScript and StarterCharacterScripts, here's what the whole script should look like:

EDIT: The positioning was a bit off on the first version, so I made another version that has the correct orientation and positioning, the math is a bit more complicated than the first version.

local RunService = game:GetService("RunService")
local Players = game:GetService("Players")

local LocalPlayer = Players.LocalPlayer
local Mouse = LocalPlayer:GetMouse()

repeat wait() until LocalPlayer.Character
local Character = LocalPlayer.Character

local function pointarmto(Torso, Target, c0, c1) 
	local midPoint = (Torso.CFrame * CFrame.new(c0)).p --offset position 
	local Direction = (midPoint - Target).unit 
	local modifyOrientation = CFrame.Angles(math.pi/2, 0, 0) 
	local RightAngle = CFrame.Angles(0, 0, math.pi) 
	local offset = CFrame.new(c1) 
	local targetCFrame = CFrame.new(midPoint, midPoint + Direction)* modifyOrientation * offset * RightAngle
	return targetCFrame
end

RunService.Heartbeat:Connect(function() -- update every Frame
	Character["Right Arm"].LocalTransparencyModifier = 0
	local armJoint = Character.Torso:FindFirstChild("Right Shoulder")
	local c0 = armJoint.C0.p -- arm center
	local c1 = armJoint.C1.p 
	armJoint.Part1 = nil
	Character["Right Arm"].Anchored = true
	local torso = Character.Torso
	local target = Mouse.Hit.p 
	local cframeT = pointarmto(torso, target, c0, c1)
	
	Character["Right Arm"].CFrame = cframeT	
end)

Hope I helped, if you have any questions please let me know!

3 comments

0

I probably need to research more about these... offsets and motor6d. but anyways thankyou :D

0

and also there seems to be some math problems that need adjustments

0

edited, corrected the orientation

This discussion is preserved as it was. New answers, comments, and votes are closed.