While Bezier curves are a good method for knowing the direction of a projectile, they donât always make sense. If youâre firing horizontally, itâd act how youâd expect, but if you ever ever fired in a declined angle, it would travel backwards in some fashion.
Projectile motion
Projectile motion is something thatâs been well-documented and explored by coders, mathematicians, and physics enthusiasts; so something called SUVAT exists. Itâs a collection of equations that allow us to determine properties of a projectile in motion. Itâs velocity, itâs distance from the origin, itâs speed, etc.
This is if you want a system that allows for drop. If youâre looking for something that will always fire a projectile linearly, you simply leave out the portion of the equation that deals with drop.
Lots of projectiles
So with a basic understanding of SUVAT under your belt, youâll want to experiment with getting initially a single projectile that works. Get it running on both your client, and on your server. Theres a couple of different approaches to handling something like this, but the most common way is that each client creates itâs own projectile, and tells the server when it fired, where it fired from, and the direction its traveling. The server then takes this data, and sends it to other clients, who then take that data and make a fake projectile. The original client is then in charge of handling all the collision data; where did it hit, when did it hit, what did it hit?
While we like to think of our players as being nice and trustworthy people, theres always a few bad apples. Hackers can now take these collisions and spoof them, by saying âThis projectile has interacted with this object at this point.â. The server then gets the hit confirmation and trusts it and says A-OK. But remember those SUVAT equations we were talking about? The client isnât the only place they can be checked. A really good method of checking if a collision actually happened is by seeing if the collision occurred along the path of the projectile. If the collision didnât happen anywhere on itâs path, then itâs likely it never happened at all. If the collision did happen, but the projectile is saying it happened at 0.1 seconds, instead of the 12 seconds it should have taken, then itâs likely that the player is either lagging, or is hacking. This is where something called Server Authority and Sanity Checks come in.
Sanity Checks/Server Authority
This is your hack detection essentially. As all data is being passed through the server, you can analyze it. The main point of a Sanity Check is to say whether or not something actually makes sense. As with the previous mentions, these mean that you can look at the data being given VS the data you should have gotten. Itâll take some tweaking, and will require a bit of understanding for what youâre looking to check, but it will result in a system thats slightly more difficult to manipulate.
Once the server says that itâs a-ok, then itâll prompt the clients to do whatever collision needed to have happened at any given point. And if the projectile is rejected? Just prompt each client to delete the bullet.
Performance
This is slightly more tricky. There are loads of different performance tips for this. Some people use parts that always exist, but thousands and thousands of units outside of the playing area, because moving a part vs creating a part is more performant. Phantom Forces uses (used? Itâs been a while) a UI elements to create them, to give them a very unique appearance.
In terms of collisions, while Robloxâs in-built Raycasting is great, it can dip here and there. Itâs often a good idea to break up the detection calls by a few frames, or limit the amount of projectile collision checks youâre doing every frame. Once you start getting projectiles over longer distances and faster, these start getting quite taxing. You could also use Parallel Luau to approach this, but Iâve not yet messed with that.
If youâre looking for something thatâs physics-based, SUVAT is the way to go to determine how each projectile should start. If youâre looking for something thats code-based, SUVAT is still the way to go, but will be incredibly useful on both client/server.
Oh and if you do code-based projectiles, try and make sure that all projectiles are moved using BulkMoveTo. It moves all objects thatâre referenced within it at once, which is cheaper than updating individual object cframe properties.
Hope thatâs useful, and do look into SUVAT! I made a projectile system that Iâve been running with for years and I didnât realize how close to these equations I was!