Help with aiming towards the mouse

I’m not good with CFrames, and i tried making something like arm aim towards the mouse with gun well after some tries it still didn’t turn out to work. So does anyone know how i actually could make this work?

my script:

local runservice = game:GetService("RunService")

local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()

local char = plr.Character or plr.CharacterAdded:Wait()
local torso = char:WaitForChild("Torso")
local rshoulder = torso:WaitForChild("Right Shoulder")

local connection = runservice.Heartbeat:Connect(function()
	local direction = mouse.Hit:ToOrientation()

	local direction = Vector3.new(
		mouse.Hit.LookVector.Y,
		mouse.Hit.LookVector.X,
		mouse.Hit.LookVector.Z)

	rshoulder.C1 = CFrame.lookAt(rshoulder.C1.Position, rshoulder.C1.Position + direction)
end)
1 Like

local Direction = (rshoulder.C1.Position - mouse.Hit.Position).Unit

1 Like

you’re using CFrame.lookAt, so your 2nd argument should be the mouse’s position instead of “rshoulder.C1.Position + direction”. Also because joint C0/C1 uses relative CFrames, you need to convert mouse’s CFrame relative to the joint’s C0/C1. Here’s a link on how to do it: How can I make my character's head, point towards another player's head? - #2 by dthecoolest

your code should look something like this:

local rightArm = char:WaitForChild("Right Arm")

local function worldCFrameToC0ObjectSpace(motor6DJoint, worldCFrame)
	local part1CF = motor6DJoint.Part1.CFrame
	local c1Store = motor6DJoint.C1
	local c0Store = motor6DJoint.C0
	local relativeToPart1 =c0Store*c1Store:Inverse()*part1CF:Inverse()*worldCFrame*c1Store
	relativeToPart1 -= relativeToPart1.Position
	local goalC0CFrame = relativeToPart1+c0Store.Position--New orientation but keep old C0 joint position
	return goalC0CFrame
end

local connection = runservice.Heartbeat:Connect(function()
	local goalCF = CFrame.lookAt(rightArm.CFrame.Position, mouse.Hit.Position)
	rshoulder.C0 = worldCFrameToC0ObjectSpace(rshoulder, goalCF) * CFrame.Angles(0,0, math.deg(-90))
end)
1 Like