How to make gun recoil/Camera shake?

Hello. I decided to add recoil for my weapon, but it didn’t work out very well. I just changed the CameraOffset, but it turned out somehow abruptly… Please help to make the recoil smooth.
I’m think need only local script:

local plr = game.Players.LocalPlayer
local activated = false
local tool = script.Parent

repeat wait(0) until plr.Character
local char = plr.Character
local Hum = char.Humanoid
local reloading = tool:WaitForChild("CanReload")
local ammo = tool:WaitForChild("Ammo")
local maxammo = tool:WaitForChild("MaxAmmo")
local rate = .1
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local uis = game:GetService("UserInputService")

script.Parent.Activated:Connect(function()
	if player.Character.Humanoid.Health <= 0 then
		tool:Destroy()
	end
	activated = true
	while wait() and activated == true do
		if ammo.Value > 0 then
			local shell = game.ReplicatedStorage.Shells.Rifle:Clone()
			shell.Position = tool.Handle.ShellEjectPoint.WorldPosition
			shell.CFrame = tool.Handle.ShellEjectPoint.WorldCFrame * CFrame.fromEulerAnglesXYZ(1.5,0,0)
			shell.Parent = workspace
			shell.Velocity = tool.Handle.CFrame.lookVector * 35 + Vector3.new(math.random(-10,10),20,math.random(-10,20))
			shell.RotVelocity = Vector3.new(0,200,0)
			script.Parent.Fire:FireServer(mouse.Hit, rate)
			reloading.Value = false
			wait(0.1)
		else
			script.Parent.CanFire.Value = true
		end
	end
end)
script.Parent.Deactivated:Connect(function()
	activated = false
end)

tool.Unequipped:Connect(function()
	reloading.Value = false
	activated = false
end)

uis.InputBegan:Connect(function(input, processed)
	if script.Parent.Parent == plr.Character then
		if input.KeyCode == Enum.KeyCode.R and reloading.Value == false and ammo ~= maxammo then
			activated = false
			tool.Reloading:FireServer()
		end
	end
end)

tool.Equipped:Connect(function()
	
end)

Notice: I’m used

Hum.CameraOffset = Vector3.new(math.random(-1,1),math.random(-1,1),math.random(-1,1))

Thanks.

4 Likes

You could use TweenService to make it smooth.

3 Likes

Thank you, I really forgot about the Tween service.

Sounds weird, but springconstraints can do a good job aswell

1 Like

But, how im make? Im really bad programmer.
I’m can make part 1 and part 2 and make motor6d with handle. And tween camera part 1 to part 2, but this bad idea.

I haven’t studied it yet, but I’ll make a note of it.

You can make the offset smaller to make it less abrubt.
However, notice how math.random only supports full numbers, so you cant just do math.random(-10,10), instead you would need to do this:

Hum.CameraOffset = Vector3.new(math.random(-10,10)/10,math.random(-10,10)/10,math.random(-10,10))

What this does is:

  • get a number from -10 to 10
  • divide that number by 10

So lets say the number you get is 5. Divide it by 10, you get 0.5

Thanks! That is, if I divide, the camera will start to turn less quickly.

I checked it works quite well, but I need the camera to turn up like cs go.

Please do not use wait().

Not too sure about this one, you could try doing:

workspace.CurrentCamera.CFrame = workspace.CurrentCamera.CFrame * CFrame.Angles(math.rad(10),0,0)

This is untested, but I think it should work

4 Likes

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