Y
Y
yraiv2022-02-05 13:25:07
Unity
yraiv, 2022-02-05 13:25:07

How to determine distance between multiple objects in 2d?

TD game. It is necessary in some way to make the towers shoot at an object that is closer to the finish line. I tried first to capture the object and until he dies - to beat him. There were disadvantages of this method. Then I decided to make a list with enemies and record them when they enter the area of ​​effect and remove them from the list when they leave / die, but if the object entered, for example, the second one, and the third one overtook it when it was already in the area of ​​​​the tower, then after killing the first object, the tower will switch to the one that entered the second, although the third object is already closer to the finish line and this is a clear flaw.
So the question is, how can you implement path counting?
The game is 2d, the road is curved, somewhere up, somewhere down, different rounds and, accordingly, different roads. How do they implement this in general, can anyone do it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mimocodil, 2022-02-06
@yraiv

Your monsters move through the waypoints in a certain order. This means that each monster must have a state that indicates where the monster is currently striving. Assuming you have the waypoints in an array, then the state is the index of the next waypoint. In this case, you can take all the monsters with the maximum index, and use Vector3.Distance to determine the distance from the target waypoint of all the monsters being checked. Then choose the smallest of them.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question