Bullet spawns a little off when turret moves

Hi there, plain and simple turret demo I’m making for when the real turret models are made. It’s an AA-Style gun with a horizontal and vertical traversal using HingeConstraints for both (set to Motor)

As of now I’m simply just trying to align the bullet with the designated position I’m trying to put it in and it works when the turret is stationary, however when it moves you’ll see in the video it’s a little behind. Why is this and how can I counter it?

This script contains all info on any movement, along with the firing system

local ELEVATION = Enum.KeyCode.W
local DEPRESSION = Enum.KeyCode.S

local RIGHT_TRAVERSE = Enum.KeyCode.D
local LEFT_TRAVERSE = Enum.KeyCode.A

local FIRE = Enum.UserInputType.MouseButton1
local STOP_FIRE = Enum.UserInputType.None

local current_traverse_velocity = 0
local current_elevation_velocity = 0

local firing = false

game.ReplicatedStorage.GunControls.OnServerEvent:Connect(function(plr, control, action, model)
	if (model == script.Parent.Name) then --Makes sure message is sent to proper turret
		if (control == FIRE) then
			firing = true
			
			while (firing) do
				local bullet = script.Parent.Bullet:Clone()
				bullet.Parent = script.Parent
				bullet.CFrame = script.Parent.L_Round_Pos.CFrame + Vector3.new(0,0.5,0) --The 0.5 is to be able to see the round for developing
				bullet.Transparency = 0
				task.wait(0.5)
			end
		
		elseif (control == STOP_FIRE) then
			firing = false
		
		elseif (control == RIGHT_TRAVERSE) then
			if (action == "StartMoving") then
				script.Parent.Upper["Horizontal Traversal"].AngularVelocity = -1
				current_traverse_velocity = -1
			elseif (action == "StopMoving" and current_traverse_velocity == -1) then
				script.Parent.Upper["Horizontal Traversal"].AngularVelocity = 0
			end

		elseif (control == LEFT_TRAVERSE) then

			if (action == "StartMoving") then
				script.Parent.Upper["Horizontal Traversal"].AngularVelocity = 1
				current_traverse_velocity = 1
			elseif (action == "StopMoving" and current_traverse_velocity == 1) then
				script.Parent.Upper["Horizontal Traversal"].AngularVelocity = 0
			end
		
		elseif (control == ELEVATION) then
			
			if (action == "StartMoving") then
				script.Parent.Stabilizer["Gun Elevation"].AngularVelocity = 1
				current_elevation_velocity = 1
			elseif  (action == "StopMoving" and current_elevation_velocity == 1) then
				script.Parent.Stabilizer["Gun Elevation"].AngularVelocity = 0
			end
		
		elseif (control == DEPRESSION) then
			if (action == "StartMoving") then
				script.Parent.Stabilizer["Gun Elevation"].AngularVelocity = -1
				current_elevation_velocity = -1
			elseif  (action == "StopMoving" and current_elevation_velocity == -1) then
				script.Parent.Stabilizer["Gun Elevation"].AngularVelocity = 0
			end
		end
	end
end)

2 Likes

Make it so the client sends the CFrame of the shooting part to the server. As the server gets the client info little bit delayed, perhaps use remote event

I have a gun script where the player can move their right arm with their mouse while in third person, and I send the CFrame of the shoot part (gun hole) to the server

Example - RemoteEvent:FireServer(shootPart.CFrame)

1 Like

There’s a chance the issue is that line

bullet.CFrame = script.Parent.L_Round_Pos.CFrame + Vector3.new(0,0.5,0)

needs to be changed to

bullet.CFrame = script.Parent.L_Round_Pos.CFrame * CFrame.new(Vector3.new(0,0.5,0))

It’s more likely the actual problem the server-sided controls are the issue. You can probably feel a delay whenever you try to input anything, right? The reason that happens is because there’s a delay between the client sending a signal to the server and vice versa.

Basically, you should make the turret directly controlled on the client and the server side will replicate it’s movements so the other clients will see it. For the bullets, you’ll want to do basic client-sided projectiles/attack calculations.

2 Likes

Ok so it does work based off what you’ve said, however I don’t want to send a bunch of remote events especially for the higher fire-rate guns, is there a workaround for this?

I mean the main reason for concern is optimization obviously, especially because multiple turrets might be firing at once.

1 Like

Really depends on just how fast the turrets fire. The majority of shooter games fire a remote event per bullet anyways. Most weapons should shoot below 20 rounds per second, though if you want to go over that, the weapons should be somewhat uncommon so too many people aren’t using it at once.

Anyways, a common trick is to have weapons like miniguns to shoot extra fake bullets. These are fired after an appropriate delay from each real shot, without using another signal to detect if the client is still shooting.

2 Likes

Ah okay, thank you. Also this is just in general is there any good direct source for optimization? Or is it just more of a dig 'til you find it?

1 Like

There’s no magic bullet to solve this one, excuse the pun. Also, if you think things are bad in Studio, wait until you have actual client-server-client roundtrip delays on production, it will be so much worse.

Shooters nearly always render the bullet VFX on the client, in sync with RenderStep, and then just send the relevant positions and directions to the server in a RemoteEvent. The server then runs its own version of the projectile kinematics, as well as validating the client data (making sure the client account of where the turret was pointed is not too far different from the server account that it might be exploiting).

1 Like

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