Need help with camera And CFrame manipulation

  1. What do you want to achieve?
    Make the gun movement while aiming smooth and not erratic.

  2. 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?

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

1 Like

try this

  1. firstPerson = true
    camera.CameraSubject = hum
    local headPos = head.Position
    local camCFrame = camera.CFrame
    camera.CFrame = CFrame.new(camCFrame.Position.X, headPos.Y, camCFrame.Position.Z) * CFrame.Angles(0, camCFrame.Rotation.Y, 0)
    local cf = camera.CFrame * CFrame.new(0, 0, -2) * CFrame.Angles(math.rad(180), 0, math.rad(270))
    local Difference = handle.CFrame:ToObjectSpace(cf)
    local aimCFrame = lens.CFrame
    local gWCFrame = gripWeld.C0
    local gPCFrame = gripPart.CFrame
    gripWeld.C0 = Difference * aimCFrame:ToObjectSpace(gPCFrame)

  2. 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 tweenInfoRecoil = TweenInfo.new(0.2, Enum.EasingStyle.Sine, Enum.EasingDirection.Out)
    local tweenRecoil = TWNS:Create(camera, tweenInfoRecoil, {CFrame = targetCFrame})
    tweenRecoil:Play()

I see that you tried to isolate the Y angle of the camera and only use it in rotating the gun around the player (Which is something I tried and makes sense to me) but it locks the player to face one way and I can not turn.

1 Like

then I dunno how to fix it any other way, lol

Maybe make the CFrame relative to the player?

Ah, it need to be relative to the camera. Not sure how I would make it relative to the char.

In order to change the current camera you need to add

camera.CameraType = Enum.CameraType.Scriptable

The current code works just with issues, I will experiment with the scriptable camera type!