R6 Point To Mouse.Hit Needed (But with working anims)

Hello! I’m trying to make the R6 arm face the mouse.Hit for my game, which is in third person camera lock.

Since I’m not really that good at angles, I looked around for some code that I could modify for my needs. I found this code:

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

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

--Weld for the cframing plus motor6ds are out of my league and animations will still work like nothing happen to it

local armWeld = Instance.new("Weld")
armWeld.Part0 = char.Torso
armWeld.Part1 = char:WaitForChild("Right Arm")
armWeld.Parent = char

--pi/2 is also known as 90 deg.(just wanted to point this out lol)

local piDiv2 = math.pi/2

RunService.Heartbeat:Connect(function()
	local CFraming = CFrame.new((char.Torso.CFrame * CFrame.new(1.5, 0.5, 0)).Position, mouse.Hit.Position) * CFrame.new(0, 0, -0.5) * CFrame.Angles(piDiv2, 0, 0)
	armWeld.C0 = char.Torso.CFrame:ToObjectSpace(CFraming)
end)

The main problem I have with this code is that I can’t animate with it since its a weld. I instead tried modifying it to instead change the C0 of the motor6d and using an animation to keep the arm still.

local rightShoulder = char.Torso:WaitForChild("Right Shoulder")
local defaultC0 = rightShoulder.C0

local piDiv2 = math.pi / 2
local lerpSpeed = 0.3 

RunService.Heartbeat:Connect(function()

    if canPointToMouse == true then
        local targetCFrame = CFrame.new(char.Torso.Position, mouse.Hit.Position) * CFrame.new(1.5, 0.5, 0) * CFrame.Angles(piDiv2, 0, 0)

        local targetC0 = char.Torso.CFrame:ToObjectSpace(targetCFrame)

        rightShoulder.C0 = rightShoulder.C0:Lerp(targetC0, lerpSpeed)
    else
        rightShoulder.C0 = rightShoulder.C0:Lerp(defaultC0, lerpSpeed)
    end
end)

But since I’m still terrible at angles, I instead got this

Sorry if whatever I said doesn’t make any practical sense. This is my first post and my first roblox project :pensive:

You will want to multiply the original C0 with the rotation of the new one to preserve the proper rotation.

Try rightShoulder.C0 = rightShoulder.C0:Lerp(defaultC0 * targetC0.Rotation, lerpSpeed) instead.

This doesn’t work either, unfortunately.

image

I disabled the animate to see if this was because of the code or if it was because of the idle animation, but it still didn’t work.

I think the reason its not working is because of the math in the code, and as I said before, I suck at angles. :confused:

local targetCFrame = CFrame.new(char.Torso.Position, mouse.Hit.Position) * CFrame.new(1.5, 0.5, 0) * CFrame.Angles(piDiv2, 0, 0)

local targetC0 = char.Torso.CFrame:ToObjectSpace(targetCFrame)

Try changing CFrame.Angles(piDiv2, 0, 0) to CFrame.Angles(0, math.rad(90), 0)


This is alot closer to what i wanted but it still doesn’t work :sob:

EDIT:

I forgot to turn off animate and also retain the proper rotation like you said earlier, but it still doesn’t work:

Thanks for helping, though!

I’m guessing you have an idle animation for the tool that makes the arm look in a different direction than the default tool idle.

Try this instead: CFrame.Angles(math.rad(90), math.rad(90), 0)

If this doesn’t work, continue tweaking the CFrame.Angles in 90 degree increments until you reach your desired result.

Also, you don’t need to multiply it with the original C0 like I originally stated in this use case. Go back to what you were using in the first video and just modify the CFrame.Angles.

1 Like

Thanks alot! I also realized that I forgot to add an offset to the CFrame of the torso when I changed it back, so it wasn’t my idle animation. Here’s my final code:

local rightShoulder = char.Torso:WaitForChild("Right Shoulder")
local defaultC0 = rightShoulder.C0

local lerpSpeed = 0.3

RunService.Heartbeat:Connect(function()

    if pointToMouse == true then
		local targetCFrame = CFrame.new(char.Torso.CFrame * CFrame.new(1, 0.5, 0).Position, mouse.Hit.Position) * CFrame.new(0, 0, -0.5) * CFrame.Angles(math.rad(90), math.rad(90), 0)

        local targetC0 = char.Torso.CFrame:ToObjectSpace(targetCFrame)

		rightShoulder.C0 = rightShoulder.C0:Lerp(targetC0, lerpSpeed)
    else
        rightShoulder.C0 = rightShoulder.C0:Lerp(defaultC0, lerpSpeed)
    end
end)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.