Hello!
I’ve recently created a revolver, however i came up with the problem.
Usually in Third Person aimming. You need your arm to point to the direction of the target.
This can be done using IKControl this is an example.
However it seems that animation doesnt work with IKControls. So I changed to Motor6d. Now heres the problem:
I must admit that Im not good at calculating CFrame, so its very hard to calculate the correct CFrame to use for this scenario and Im out of options but to ask some help from the forum members!
This is my code for the aimming
local rightshoulderc0 = rightshoulder.C0
game:GetService("RunService").RenderStepped:Connect(function()
if mouse2down then
-- Get the direction from the shoulder to the mouse hit position
local mouseHit = mouse.Hit.p
local shoulderPosition = (rightshoulder.Part0.CFrame * rightshoulderc0).p
local direction = (mouseHit - shoulderPosition).Unit
local lookAt = CFrame.lookAt(shoulderPosition, mouseHit) -- Creates a CFrame pointing to the mouse hit
local newC0 = rightshoulderc0:ToObjectSpace(lookAt) -- Transform relative to the original C0
rightshoulder.C0 = newC0
end
end)
Mouse.Move:Connect(function()
local isMouseActive = mouse2down
if isMouseActive then
local armNames = { "Right Arm" }
for _, armName in pairs(armNames) do
if character:FindFirstChild(armName) then
local shoulderName = armName:split(" ")[1] .. " Shoulder"
local shoulderJoint = character.Torso:FindFirstChild(shoulderName)
local _, _, shoulderRotationZ = shoulderJoint.C0:ToEulerAnglesYXZ()
local aimAngle = math.asin((Mouse.Hit.p - Mouse.Origin.p).unit.y)
if armName == "Right Arm" then
shoulderJoint.C0 = shoulderJoint.C0 * CFrame.Angles(0, 0, -shoulderRotationZ) * CFrame.Angles(0, 0, (aimAngle * 0.7))
else
shoulderJoint.C0 = shoulderJoint.C0 * CFrame.Angles(0, 0, -shoulderRotationZ) * CFrame.Angles(0, 0, -(aimAngle * 0.7))
end
end
end
else
return
end
end)
Mouse.Move:Connect(function()
local isMouseActive = mouse2down
if isMouseActive then
local armNames = { "RightUpperArm" }
for _, armName in pairs(armNames) do
if character:FindFirstChild(armName) then
local shoulderName = armName:split("Upper")[1] .. "Shoulder"
local shoulderJoint = character:FindFirstChild(shoulderName, true)
if shoulderJoint then
local _, _, shoulderRotationZ = shoulderJoint.C0:ToEulerAnglesYXZ()
local aimAngle = math.asin((Mouse.Hit.p - Mouse.Origin.p).unit.y)
if armName == "RightUpperArm" then
shoulderJoint.C0 = shoulderJoint.C0 * CFrame.Angles(0, 0, -shoulderRotationZ) * CFrame.Angles(0, 0, (aimAngle * 0.7))
else
shoulderJoint.C0 = shoulderJoint.C0 * CFrame.Angles(0, 0, -shoulderRotationZ) * CFrame.Angles(0, 0, -(aimAngle * 0.7))
end
end
end
end
else
return
end
end)
I updated the scripts into something like this but as you can clearly see in the video it doesn’t work.
The reason i was updating the script is to reduced some unused lines such as the _, armName in pairs part which is not neccesarly since you used 1 table! Heres my updated code (which still works the same as the script you provided)
if character:FindFirstChild("RightUpperArm") then
local shoulderName = "RightShoulder"
local shoulderJoint = character:FindFirstChild(shoulderName, true)
if shoulderJoint then
local _, _, shoulderRotationZ = shoulderJoint.C0:ToEulerAnglesYXZ()
local aimAngle = math.asin((mouse.Hit.p - mouse.Origin.p).unit.y)
shoulderJoint.C0 = (rightShoulderC0 * CFrame.Angles(math.rad(90),0,0)) * CFrame.Angles(-shoulderRotationZ,0, 0) * CFrame.Angles((aimAngle * 0.7), 0, 0)
end
end
I added math.rad(90) to make the arms turn 90 degree (aim position) and i also changed the z axis to x axis is because when i was testing. The arms were rotating in the wrong direction!
It does still see some minor errors. Please help me!
Hello! You don’t need to rotate by 90 degrees again because im assuming your animation already does that.
character = script.Parent
mouse.Move:Connect(function()
local isMouseActive = mouse2down
if isMouseActive then
if character:FindFirstChild("RightUpperArm") then
local shoulderName = "RightShoulder"
local shoulderJoint = character:FindFirstChild(shoulderName, true)
if shoulderJoint then
rightShoulderC0 = shoulderJoint.C0
local _, _, shoulderRotationZ = rightShoulderC0:ToEulerAnglesYXZ()
local aimDirection = (mouse.Hit.p - mouse.Origin.p).unit
local aimAngle = math.asin(aimDirection.y)
shoulderJoint.C0 = (rightShoulderC0 * CFrame.Angles(-shoulderRotationZ, 0, 0)) * CFrame.Angles((aimAngle * 0.7), 0, 0)
end
end
end
end)