-
What do you want to achieve?
Make the gun movement while aiming smooth and not erratic. -
What is the issue?
The gun moves uncontrolled when walking and shooting. – FIXED
And the gun sways toward the direction the player is turning, however up and down is fine.
I think this is because the Char is moving with the camera, but I don’t want the script to adjust for left and right that way when the Char turns the gun and camera turn naturally with it.
How do I go about fixing this?
-
What solutions have you tried so far?
Messing with CFrame rotations, tweaking code.
My aiming script:
firstPerson = true
camera.CameraSubject = hum
local headPos = head.Position
camera.CFrame = CFrame.new(camera.CFrame.Position.X, headPos.Y, camera.CFrame.Position.Z) * camera.CFrame.Rotation
-- adjust camera to work with crouching
local cf = camera.CFrame * CFrame.new(0, 0, -2) * CFrame.Angles(math.rad(180), 0, math.rad(270))
--fix gun orientation.
local Difference = handle.CFrame:ToObjectSpace(cf)
--getting the dif between the handle and camera, this is because the weld is contained in handle, even if the handle isnt moving.
local aimCFrame = lens.CFrame
local gWCFrame = gripWeld.C0
local gPCFrame = gripPart.CFrame
gripWeld.C0 = Difference * aimCFrame:ToObjectSpace(gPCFrame)
My recoil logic that’s controlled by another script:
local currentCam = camera.CFrame
local randomY = math.random(-gunStats.Recoil, gunStats.Recoil)
local targetCFrame = currentCam * CFrame.Angles(math.rad(gunStats.Recoil / 15), math.rad(randomY / 18), 0)
local recoilTime = 0.00001
local steps = 3
local stepDuration = recoilTime / steps
for i = 1, steps do
local alpha = i / steps
camera.CFrame = currentCam:Lerp(targetCFrame, alpha)
task.wait(stepDuration)
end
Video of gun swaying and moving erratically.
EDIT: Using Lerping for recoil instead of tweening makes the gun not fling all over the screen. Can someone explain why?
However the gun still sways which is still making the shooting while aiming look slightly abnormal.