I want to achieve a working recoil for my turret barrel.
main issue is that the recoil which i currently have in the script below is buggy and is very unpredictable, so much that the barrel literally recoils by 100 studs and then it compounds, so first recoil barrel travels back maybe 50 studs 2nd recoil it travels back 100 studs and then 200 studs, 400 etc.. It just keeps getting further and further, Well thats not really important but still, the recoil is clearly messed up.
I tried finding help on the devforum, but most of them were irrelevant or were too complex and did not work for me.
Client script (Player has network ownership of the turret thats why):
local turretModule = require(game.ReplicatedStorage.ModuleScript)
local base = turretModule.Base
local barrel = turretModule.Barrel
game:GetService("RunService").RenderStepped:Connect(function(dt)
game.ReplicatedStorage.RemoteEvent.OnClientEvent:Connect(function()
barrel.Position += barrel.CFrame.LookVector * 1 -- recoil (buggy)
end)
turretModule.TargetFinder()
if turretModule.PlayerTarget then
local target = turretModule.PlayerTarget
turretModule.PitchHingeFunction()
if turretModule.PitchHingeDirection then
barrel.pitchHinge.TargetAngle = math.deg(turretModule.PitchHingeDirection)
end
turretModule.YawHingeFunction()
if turretModule.YawHingeDirection then
base.yawHinge.TargetAngle = math.deg(turretModule.YawHingeDirection)
end
end
end)
my turret has hinge constraints which I think is messing up the recoil effect
The issue is that you add a listener to the RemoteEvent.OnClientEvent event every frame. If you move that connection outside of RenderStepped function, your code shouldn’t have any issues.
local turretModule = require(game.ReplicatedStorage.ModuleScript)
local base = turretModule.Base
local barrel = turretModule.Barrel
game.ReplicatedStorage.RemoteEvent.OnClientEvent:Connect(function()
barrel.Position += barrel.CFrame.LookVector * 1 -- recoil (buggy)
end)
game:GetService("RunService").RenderStepped:Connect(function(dt)
turretModule.TargetFinder()
if turretModule.PlayerTarget then
local target = turretModule.PlayerTarget
turretModule.PitchHingeFunction()
if turretModule.PitchHingeDirection then
barrel.pitchHinge.TargetAngle = math.deg(turretModule.PitchHingeDirection)
end
turretModule.YawHingeFunction()
if turretModule.YawHingeDirection then
base.yawHinge.TargetAngle = math.deg(turretModule.YawHingeDirection)
end
end
end)
Thank you for pointing that out, It fixxes the issue of my barrel going back by a hundred studs every frame. Is there a way to tween the barrel so that it slides back as a smooth recoil effect?
Well tweens wouldn’t really work as the barrel also needs to move around in other directions. You could use recoilSpeed and returnSpeed variables to control how fast it moves. We can use our current, last, and target recoil values to predict how much we should move the barrel. The recoil variable is the distance the barrel moves back.
local turretModule = require(game.ReplicatedStorage.ModuleScript)
local base = turretModule.Base
local barrel = turretModule.Barrel
local currentRecoil = 0
local lastRecoil = 0
local targetRecoil = 0
local returnSpeed = 1
local recoilSpeed = 10
local recoil = 1
game.ReplicatedStorage.RemoteEvent.OnClientEvent:Connect(function()
--barrel.Position += barrel.CFrame.LookVector * 1 -- recoil (buggy)
targetRecoil = recoil
end)
game:GetService("RunService").RenderStepped:Connect(function(dt)
if targetRecoil > 0 or currentRecoil > 0 then -- some recoil is happening
local change = 0
if targetRecoil > currentRecoil then -- need to move backward
change = recoilSpeed*dt
elseif targetRecoil < currentRecoil then -- need to return/reduce recoil
change = returnSpeed*dt
else -- reached the max recoil back, time to return to our starting position
targetRecoil = 0
end
-- make sure we don't push the barrel outside the bounds
currentRecoil = math.clamp(change+currentRecoil, 0, recoil)
-- use the differences in recoil incase the change would push us out of bounds
barrel.Position += barrel.CFrame.LookVector * (currentRecoil-lastRecoil)
lastRecoil = currentRecoil
end
turretModule.TargetFinder()
if turretModule.PlayerTarget then
local target = turretModule.PlayerTarget
turretModule.PitchHingeFunction()
if turretModule.PitchHingeDirection then
barrel.pitchHinge.TargetAngle = math.deg(turretModule.PitchHingeDirection)
end
turretModule.YawHingeFunction()
if turretModule.YawHingeDirection then
base.yawHinge.TargetAngle = math.deg(turretModule.YawHingeDirection)
end
end
end)
If this doesn’t work, send the errors or the file so I can help debug. Kinda hard to test without the file.
Tweens can work. Just have the visual element of the barrel (a copy of the barrel) parented to the physical/functional barrel (the part you have now, but invisible) and animate the recoil on the visual element.
Tangent about clientside
You can even set it up to run completely clientsided by having the visual part be created on the clientside and telling it to animate using a single RemoteEvent (which can even be unreliable if you have multiple of these in a TD or something). This would give a lot less work for the server, allow tuning animation performance for each client, and use a lot less networking.
I don’t know what this technique is called but the pros use it all over.
You would need physics constraints for a tween to work for recoil and continuous aiming adjustments. Once you start the recoil tween, you wouldn’t be able to move the barrel. Also, you can look at the code and see it’s already client side.
Got it working, I just forgot to make the change be negative when the barrel is returning. Here’s the TurretVisuals LocalScript, the only changes I made:
local turretModule = require(game.ReplicatedStorage.ModuleScript)
local base = turretModule.Base
local barrel = turretModule.Barrel
local currentRecoil = 0
local lastRecoil = 0
local targetRecoil = 0
local returnSpeed = 10
local recoilSpeed = 30
local recoil = 5
game.ReplicatedStorage.RemoteEvent.OnClientEvent:Connect(function()
--barrel.Position += barrel.CFrame.LookVector * 1 -- recoil (buggy)
targetRecoil = recoil
end)
game:GetService("RunService").RenderStepped:Connect(function(dt)
if targetRecoil > 0 or currentRecoil > 0 then -- some recoil is happening
local change = 0
if targetRecoil > currentRecoil then -- need to move backward
change = recoilSpeed*dt
elseif targetRecoil < currentRecoil then -- need to return/reduce recoil
change = -returnSpeed*dt
else -- reached the max recoil back, time to return to our starting position
print("Heading back")
targetRecoil = 0
end
-- make sure we don't push the barrel outside the bounds
currentRecoil = math.clamp(change+currentRecoil, 0, recoil)
-- use the differences in recoil incase the change would push us out of bounds
barrel.Position += barrel.CFrame.LookVector * (currentRecoil-lastRecoil)
lastRecoil = currentRecoil
end
turretModule.TargetFinder()
if turretModule.PlayerTarget then
local target = turretModule.PlayerTarget
turretModule.PitchHingeFunction()
if turretModule.PitchHingeDirection then
barrel.pitchHinge.TargetAngle = math.deg(turretModule.PitchHingeDirection)
end
turretModule.YawHingeFunction()
if turretModule.YawHingeDirection then
base.yawHinge.TargetAngle = math.deg(turretModule.YawHingeDirection)
end
end
end)
If you want to make the turret smarter and shoot where players are going to be rather than where they are right now, I can help with that as well.