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
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.
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)