How to optimize bullets?

Hey, soo i’m working on artillery game where players use artillery to fight, i made this module to calculate trajectory and return hit and all points

function Projectiles:CalculateTrajectory(Origin: Vector3, Direction: Vector3, Propertiess: TrajectoryPropertiess): {Vector3}
	local Path = {}
	local Drop = 1
	local Samples = 0
	local PreviousPoint = Origin
	
	local Hit: BasePart
	
	local RaycastParameters = RaycastParams.new()
	RaycastParameters.FilterDescendantsInstances = Propertiess.IgnoreList
	
	local startTime = tick()
	local Force = Direction * Propertiess.Power * Propertiess.AirResistance
	repeat
		local CurrentPoint = PreviousPoint + Force - Vector3.new(0, Drop * Propertiess.AirResistance, 0)
		Path[Samples] = CurrentPoint
		Drop *= 1.35
		Samples += 1
		
		local raycastResult = workspace:Raycast(PreviousPoint, (CurrentPoint - PreviousPoint), RaycastParameters)
		if raycastResult then
			Hit = raycastResult.Instance
			break
		end
		
		PreviousPoint = CurrentPoint
	until tick() - startTime > Propertiess.Lifespan or Samples > Propertiess.MaxSamples or Hit ~= nil 
	
	return Hit, Path
end

My question is, will it lag when used? i need to run this calculation on client to create visual bullet, then i need to run it again on server to calculate collisions, and at the end i have to run it on every other client to get points soo i can lerp bullet

Isn’t it too much?

1 Like

I’m not too sure about ballistics but I know it’s complicated. Honestly, I would’ve expected it to be more. If you want to optimize it between the server and client, I suggest calculating it first on one side (probably server side) and then just giving the points of the trajectory to the other side.

problem is this will create delay for slow network players, another way is to calculate everything on client, but then i still need to run this function on every other client (sending 50 vectors isn’t a very good idea)

At that point, couldn’t you just calculate it on the client and then run it on the server? It should replicate to the other clients. Also, an issue with calculating it all on the client is you risk exploits.
I know some games do the calculations on the server side and that’s why they sometimes return errors like “Invalid shot” if you’re too laggy. Is this for handguns or for artillery vehicles?

It simply calculates trajectory and returns points and hitted part, problem is that idk if handling it on client and only security check on server would be better for me, because i need to lerp bullet using those points, and doing it on server may be not that performant friendly

Well, I think I’d do it how you first described: by calculating it on the client that fired the shot, then calculating the global trajectory on the server and returning that trajectory to the other clients. If you calculate it on every client, it would not be consistent, and if you calculate it solely on the client that fired the shot, your game would be very vulnerable to exploits. I believe this is how most actual games do it (at least for handguns).

problem is i can’t send entire path, i need to run functions three times with same direction, origin and parameters