Need help with 3rd person cam locked recoil system

Hello so i have this 3rd person camera system which updates the cam’s look vector every frame which presents problems when trying to make recoil for the camera, my question is if there is a good way to make recoil work with this, thanks in advance.

3rd person camera script:

local players = game:GetService("Players")
local contextactionservece = game:GetService("ContextActionService")
local RunService = game:GetService("RunService")
local UserInput = game:GetService("UserInputService")
local TweenServ = game:GetService("TweenService")
--------
local Camera = workspace.CurrentCamera
local player = game.Players.LocalPlayer
local cameraOffset = Vector3.new(1.5, 2, 12)
local OGcameraOffset = Vector3.new(1.5, 2, 12)
local cameraOffset2 = Vector3.new(-1.5, 2, 12)
local cameraOffset2CFrame = CFrame.new(-1.5, 2, 12)
local cameraOffsetAimRight = Vector3.new(2.5, 2.5, 5)
local cameraOffsetAimLeft = Vector3.new(-2.5, 2.5, 5)
local isRight = true
local isBeingChanged = false
local HeartBeat = RunService.Heartbeat
local deltaTime = HeartBeat:wait()
local IsAiming = false
local AimConformed = false
-----------------------------------------------------------------------

-----------------------------------------------------------------------
player.CharacterAdded:Connect(function(character)
	-- actual camera function, i.e positioning

	local humanoid = character:WaitForChild("Humanoid")
	local rootPart = character:WaitForChild("HumanoidRootPart")

	local cameraAngleX = 0
	local cameraAngleY = 0



	local function playerInput (actionName, inputState, inputObject)
		if inputState == Enum.UserInputState.Change then

			cameraAngleX -= inputObject.Delta.X
			cameraAngleY = math.clamp(cameraAngleY - inputObject.Delta.Y * 0.4, -75, 75)

		end
	end

	contextactionservece:BindAction("PlayerInput", playerInput, false, Enum.UserInputType.MouseMovement, Enum.UserInputType.Touch)

	RunService:BindToRenderStep("CameraUpdate", Enum.RenderPriority.Camera.Value, function()
	
		local startCFrame = CFrame.new(rootPart.CFrame.Position) * CFrame.Angles(0, math.rad(cameraAngleX), 0) * CFrame.Angles(math.rad(cameraAngleY), 0, 0)
		local cameraCFrame = startCFrame:PointToWorldSpace(cameraOffset)
		local cameraFocus = startCFrame:PointToWorldSpace(Vector3.new(cameraOffset.X, cameraOffset.Y, -1000000))

		-- sensitivity

		UserInput.MouseDeltaSensitivity = 0.5

		--
		Camera.CFrame = CFrame.lookAt(cameraCFrame, cameraFocus)
		-- shift lock ----------------------------------------------------------------
		
		local CanLock = character:WaitForChild("AnimationValuesFolder").MouseLockEnabled
		if CanLock.Value == true and isBeingChanged == false then
			
			IsAiming = true
			local lookingCFrame = CFrame.lookAt(rootPart.Position, Camera.CFrame:PointToWorldSpace(Vector3.new(0, 0, -1000000)))
			rootPart.CFrame = CFrame.fromMatrix(rootPart.Position, lookingCFrame.XVector, rootPart.CFrame.YVector)
			humanoid.AutoRotate = false 
			
			
		else
			humanoid.AutoRotate = true
		end
		
		-----------------------------------------------------------------------------
		-- clipping
		local Character = game.Players.LocalPlayer.Character
		local CharacterHead = Character:WaitForChild("Head")
		local CameraRay = Ray.new(CharacterHead.Position, Camera.CFrame.Position - CharacterHead.Position)
		local Ignore = {Character, game.Workspace.PartsToIgnoreInCam}

		local HitPart, HitPosition = game.Workspace:FindPartOnRayWithIgnoreList(CameraRay, Ignore)

		Camera.CFrame = (Camera.CFrame - (Camera.CFrame.Position - HitPosition)) + (CharacterHead.Position - Camera.CFrame.Position).Unit
			--
	end)
end) 
------------------------------------------------------------------------
local function focusControl(actionName, inputState, inputObject)
	--mouse control / locking center

	Camera.CameraType = Enum.CameraType.Scriptable

	UserInput.MouseBehavior = Enum.MouseBehavior.LockCenter
	--UserInput.MouseIconEnabled = false   //MAKE MOUSE GO ICON GO POOF

	contextactionservece:UnbindAction("FocusControl")

end

contextactionservece:BindAction("FocusControl", focusControl, false, Enum.UserInputType.MouseButton1, Enum.UserInputType.Touch, Enum.UserInputType.Focus)
-- change shoulder ---------------------------------------
local Dbounce = false
local function inputBegan(input, gameProcessedEvent)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == Enum.KeyCode.T then
			if not gameProcessedEvent then
				if Dbounce == false then
					Dbounce = true
					if isRight == true then
						isBeingChanged = true
						for i = 0,1, .01 do

							cameraOffset = cameraOffset:Lerp(cameraOffset2, i)
							local deltaTime = HeartBeat:Wait()

						end

						isRight = false
						isBeingChanged = false
					else
						isBeingChanged = true
						for i = 0,1, .01 do

							cameraOffset = cameraOffset:Lerp(OGcameraOffset, i)
							local deltaTime = HeartBeat:Wait()

						end

						isRight = true
						isBeingChanged = false
					end
					wait(0.5)
					Dbounce = false
				end
			end
		end
	end
end
UserInput.InputBegan:Connect(inputBegan)

PS: the recoil will be happening in a local script as it is for a tool, like a gun.

2 Likes

No, because this is manipulating the camera every frame.

how do other third person games make recoil then?