Tower Defense Projectiles?

Hello, I have a question that I’ve been trying to figure out for quite some time. How would I make projectiles for a tower defense game?

Now before you give me a simple example on how I can do it I know how to create the projectile and move it as well as some collision detection, though my question is this:
How can I make a projectile that has accurate collision but also has smooth movement?

I’ve made a projectile on the server by using a part , using linear velocity to move it and using getPartsTouchingParts for detecting collision, but it wasn’t smooth on the client.
The projectile did move but it wasn’t as smooth as the server side and the physics replication was just not really good.

Now I though of using remote events and make it fire every time a tower shot a projectile so the remote spawns a visual representation of that projectile on the client, though I thought that it would exhaust the remote event considering how if you have many towers from each player firing it, it would get bad pretty quickly.

Does anyone have any ideas on this topic? Any help is appreciated, thanks!

1 Like

To make the projectile smoother, you can set the projectile’s Network ownership to nil however, if there are too many projectiles, this could cause a lot of lag on the sever.

To work around this, instead of having the projectile on the sever, you can have the projectile on the client, and calculate the damage done on the sever. This would make it so that the sever does not need to simulate any projectiles, and there is no projectile delay for the client.

1 Like

that’s a good idea, but how exactly would I calculate the damage on the server?

send a remote event only for when the target of the tower changes

and then make projectiles on the client

that wouldn’t work because then that would only spawn one projectile on the client for each target, no?

the server sends fires the event when the target of a tower changes
then the client can calculate the cooldown and make projectiles

I don’t think that would work too well.

I have one more question though, how would the projectiles be on the client in the first place? Since the towers are on the server, how would I spawn the projectiles for each and every tower on the client for each player?
I’m thinking I could use attributes, but I don’t know how well that would go.
Any help?

why would it not work?

the same enemies would be damaged, but the timing of the projectiles would be slightly off between clients

as I already said, we can’t just fire the remote once because that would result in only one projectile being made (assuming you are saying to create a projectile for each remote fire).
That being said firing the remote too many times because of all the towers with projectiles will cause it to exhaust.

i mean to fire the remote once every time a tower changes its target

and then the client makes projectiles in a loop until the tower changes targets again

this way theres less remote events because you only send a target change, and not each time a projectile is made

the client makes more than 1 projectile for each target change since it repeatedly fires until the target changes again

There is a few ways you can do this:

  1. The sever will send a remote event every time a tower shoots a projectile. It will then calculate the amount of time it takes for the projectile to reach an enemy and update the enemy’s health accordingly. On the client, the projectile will be shown as a visual thing every time it receives a remote event. Although this does fire a lot of remote events depending on the tower type (if the tower is rapid fire or not) however, this will ensure that there is no syncing issues between client and sever.

  2. When a tower is placed, you can tell the clients, and the clients will act accordingly to the behavior of that tower. For example, when a tower is placed the client will wait a certian amount of time (cooldown) and then it’ll create a projectile accordioning to the tower’s behavior (targets first enemy, targets lowest enemy, ect.) On the sever, the same thing will happen however, instead of creating the poejectile, it will calculate the amount of time it takes for the projectile to hit the enemy and how much damage it’ll do to the enemy. The problem with this is that it can create some syncing issues with the client and sever as lag on the client or sever could throw things off.

You can also combine the 2 methods, you can use remote events for towers that shoot relativity slowly and for towers that rapid fires very quickly, you can use method 2. If you plan to use any of these methods, or both of them, let me know and I can explain more and more ways to fix the issues within each methods.

it’s not a bad idea, but there are some flaws to it for my system.

the first method is not bad, but I want my projectiles to also be able to miss, not just shoot and always hit. That’s the only reason I’m actually making the projectile anyway.

that can be calculated on the sever side, you can use ray cast if the projectile is straight or you can use various physics equations to simulate your projectile mathematically instead of physically.

Check this out if you want to know more about mathematically calculating the path of your projectile.

generally they will be straight, there might be some exceptions in the future but most of the towers with projectiles will have straight projectiles.

I don’t quite understand the 2nd method, though the first method

remote event every time a tower shoots a projectile

wouldn’t that exhaust the remote either way even with slow shooting towers. Taking into account the fact that the other players have towers that can shoot projectiles too

Yes, while having a lot of remote events being fired at once is bad for the sever, the limit for how much remote events before the sever or client gets overwhelmed is quite high. You can think of remote event as another way to send data between sever and client, similar to how data is being send every frame from each object’s position to their properties. As long as you are careful in selecting which towers sends remote events, and how much data is being sent, you will be fine. You can even decide whether you want a remote event to be fired or not via checking factors should as the sever ping and stuff.

If the projectile is straight, you can use ray cast.

You can also calculate how long it’ll take for the projectile to reach a certain spot, then wait that long and check if the enemy is within that position. If so, then the enemy has been hit and you can update the health according.

alright, I understand. Thanks a lot

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