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.