I need help with making a Prediction system for a tower defense game

  1. What do I want to achieve?
    I want to make it so projectile towers can predict where the enemy will be when their projectile gets to them. For example, a Grenadier tower that throws a grenade at the enemy position with the grenade speed being 10, but due to the enemy being able to move, it can sometimes miss it, so I want to make it throw the grenade right into the position where the enemy will be when the grenade lands.

  2. What is the issue? I simply cannot understand how to make it work. The problem there is that I need time to predict the enemy, but to get it I need to calculate distance between the tower and the enemy, which I cannot get without getting the position of the enemy.
    This is probably possible without using time and rather using the distance between the tower and the enemy & projectile speed, but I don’t really know how to make it work, which is why I’m making this post.

My enemy system works using a single number between 0 and amount of positions the map has, using this number inside a lerp() function. So, for example, when the enemy would have Alpha of 1.5, it would be put in middle of Pos1 and Pos2.
Alpha gets increased from that formula: Alpha = Alpha + ((Speed / Distance) / 50) * PassedMultiplier,
with Speed being it’s own speed, Distance being the Distance between two Positions (Pos1 and Pos2, for example), PassedMultiplier being a check of how much time actually passed by task.wait(.01) using tick().
So far I tried multiple things, but none of them worked since none of them actually predict where the enemy will be.

What I actually need is to somehow get the predicted position of the enemy using: Enemy position, Tower position, Distance between the tower and the enemy, and projectile speed. If someone can help me, thank you.

1 Like

First off, use a RunService function such as Heartbeat instead of task.wait(0.1).

Secondly, if a grenadier throws a grenade, does it always reach the target in the same amount of time, or does it reach the target in a longer amount of time when the target is further? For the first case, things are simpler, as you just need to calculate where the enemy is in N seconcs, where N is the time it takes the grenade to reach the target. You’d do that the exact same way you move the enemies every frame. And then that position is where you shoot.

I completely forgot about RunService, but there’s no point to change it rn as it already gets a multiplier of how much time actually passed.

For the grenade, it depends on the distance between the tower and the enemy (It would look way too goofy if the enemy is really close or really far away, y’know).

I recommend watching the following video, it has what you are looking for: Tower Defense - Roblox Scripting Tutorial.

use this library

1 Like

This isn’t what I needed as my enemies can change their direction whenever they’re going to the next position of the map, my enemies also do not have “Velocity” as they’re getting moved by a lerp() function. Thanks though.

I have found the solution already, thanks everyone who tried to help me

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