#Problem with cframe math for aiming system and interference from animations

1 messages · Page 1 of 1 (latest)

vivid cloud
#

Hello, this is an old problem I had with my aiming system for my gun and its a crucial part of the game.
So, basically in order to implement a time slowing system - where the aiming speed respects the time slow my character's aim needs to take to to reach where it is when the time is slowed, and the gun's muzzle must point exactly where my mouse is aiming. Due to the animations played on the player it causes interference with the cframe math and final positioning - causing very innacurate and inconsistent aiming.

This is how it is when stationary: 20250703-1530-21.7498833.mp4

While walking/Running: 20250703-1531-24.9628278.mp4

This is an example of the time-slow system interacting with the aiming for a bit more context: 20250703-1537-40.4916690.mp4

The script will be included in the messages after.

glacial lindenBOT
#

studio** You are now Level 1! **studio

vivid cloud
#
local c0 = CFrame.new(1, 0.5, 0, 0, 0, 1, 0, 1, -0, -1, 0, 0)

local pistolAim = {}

function pistolAim:Call(args)
    local player = game.Players.LocalPlayer
    local char = player.Character or player.CharacterAdded:Wait()
    local humRP = char:WaitForChild("HumanoidRootPart")
    local torso = char:WaitForChild("Torso")
    local rightShoulder = torso:FindFirstChild("Right Shoulder")
    local mouse = player:GetMouse()
    
    local uis = game:GetService("UserInputService")

    local workspace = game:GetService("Workspace")

    local raycastParams = RaycastParams.new()
    raycastParams.FilterDescendantsInstances = {char}
    raycastParams.FilterType = Enum.RaycastFilterType.Exclude
    
    local playerShiftLockModule = require(player.PlayerScripts.CustomShiftLock.SmoothShiftLock)
    
    local connection

    connection =  game:GetService("RunService").RenderStepped:Connect(function(dt)
        local muzzleWorldCFrame = char.GunModel.Muzzle.Attachment.WorldCFrame
        local mousePos = mouse.Hit.Position

        local rayOrigin = muzzleWorldCFrame.Position
        local rayDirection = (mousePos - muzzleWorldCFrame.Position).Unit

        if playerShiftLockModule:IsEnabled() then
            rayDirection = (mousePos - (muzzleWorldCFrame * CFrame.new(Vector3.new(1.75, -0.25, 0))).Position).Unit
        end

        local aimPosition = rayOrigin + rayDirection * 1000
#

        -- calculate desired world orientation for the arm
        local shoulderWorldPos = (torso.CFrame * c0).Position
        local desiredArmWorld = CFrame.lookAt(shoulderWorldPos, aimPosition)

        -- get rotation-only parts
        local torsoRotationOnly = torso.CFrame - torso.CFrame.Position
        local armRotationOnly = desiredArmWorld - desiredArmWorld.Position

        -- calculate C0 that compensates for current torso rotation
        local c0Rotation = torsoRotationOnly:Inverse() * armRotationOnly
        local rx, ry, rz = c0Rotation:ToOrientation()

        -- set C0 with proper rotation
        local targetC0 = CFrame.new(c0.Position) * CFrame.Angles(0, ry+ math.rad(90), rx + math.rad(90))

        rightShoulder.C0 = rightShoulder.C0:Lerp(
            targetC0,
            1
        )

        game.ReplicatedStorage.Remotes.AimUpdate:FireServer(
            targetC0,
            1
        )
    end)

    task.spawn(function()
        while char:FindFirstChild("Aiming") do
            task.wait()
        end
        connection:Disconnect()
    end)
end

return pistolAim

#

If anyone has any idea how to help please let me know