Camera Tweening for Recoil

Hello,

I was wondering how would i Tween the Camera’s Orientation for recoil.

So what im trying to do is create Recoil when firing a weapon, when i try tweening the camera, instead of Rotating. It goes to a certain position, I already know how to use TweenService but the camera doesnt stay attached to the play and instead goes somewhere else, what should i do?

Nothing on this post in the past 3 days.

For me the best way to do camera recoil is via TweenService. However, you cannot tween directly to the camera because TweenService does not update your camera every frame. In this situation I would make an imaginary part, apply Tweening on it then multiply its rotation to camera.

local currentCamera = workspace.CurrentCamera
local runService = game:GetService('RunService')
local localPlayer = game:GetService('Players').LocalPlayer
local tweenService = game:GetService('TweenService')
local mouse = localPlayer:GetMouse()

--Get speed and intensity value
local recoilTime = 0 --Input num here
local intensity = 0 --Input num here

--Create an imaginary BasePart
local part = Instance.new('Part')

--Detect if Left button is down
mouse.Button1Down:Connect(function()
	--Tween info
	local tweenInfo = TweenInfo.new(recoilTime, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut, 0, true)
	local upRecoil = tweenService:Create(part, tweenInfo, {CFrame = CFrame.Angles(math.rad(intensity), 0, 0)})
	local downRecoil = tweenService:Create(part, tweenInfo, {CFrame = CFrame.Angles(-math.rad(intensity), 0, 0)})
	
	--Reset part's Cframe if mouse.Button1Down when tweening is not done
	part.CFrame = CFrame.new() * CFrame.Angles(0, 0, 0)
	--Part tweening
	upRecoil:Play()
	upRecoil.Completed:Connect(function()
		downRecoil:Play()
		downRecoil.Completed:Connect(function()
			--Reset again
			part.CFrame = CFrame.new() * CFrame.Angles(0, 0, 0)
		end)
	end)
end)

--Apply recoil to Camera
runService.RenderStepped:Connect(function()
	currentCamera.CFrame *= part.CFrame.Rotation
end)

(Sorry for late reply tho, I have just figured out)

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.