- Home /
Tower Defense - Making the Player move only spesific places
Greetings everyone!
I am creating a top down tower defense game with tiles. So i have 2 tilemaps, one is for green area (for towers and players) and one is for the road of enemies. I want the make player walk in only on green area not on road area. How to approach that idea? What's the keyword and some tips for that? There is smth. to done with colliders maybe. But i couldn't understand exactly.
Answer by Llama_w_2Ls · Jul 09, 2021 at 09:11 PM
I think what you're looking for is pathfinding. This takes in a start position and an end position, and tries to find the fastest way from one point to another without touching any obstacles.
If your track/room is procedurally generated, or the course that enemies may take is dynamic, you need to use a pathfinding algorithm. There are some free 2D pathfinding assets that can help you get this set up very quick and easy. Ill link them, just ask.
However, like in the case of bloons tower defense, where all enemies take the same route, that can be precalculated before-hand, using a pathfinding algorithm is a bit overkill for that many agents. A simpler way is to use the edge collider 2D component, to draw the rough track that they take, and create a movement script that moves each agent to the next vertex of the edge collider.
Tell me which option you want to go for, and ill link the relevant scripts/resources below. @thekrocker
Answer by thekrocker · Jul 10, 2021 at 10:07 AM
Thanks a lot sir for your all information! I used path objects, so that they move towards to it, if finds, it goes the next path.
What i am trying to find is, (my game is like bloon tower defence) My character(there is a moving character with w-a-s-d to repair towers) should not be able to go Enemy road. Can i fix that with edge collider?
I'm not really understanding.
there is a moving character with w-a-s-d to repair towers
Does this mean that you are manually controlling the character? If so, it's not really pathfinding but limiting movement to certain regions of the map. You can do this using a rigidbody2D controller, and putting edge colliders outside the areas you don't want the player to walk. The rigidbody can't pass the collider, and so it will stay within the desired region.
Your answer
![](https://koobas.hobune.stream/wayback/20220613040106im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Is there a way to render in-game a sphere collider? 2 Answers
physics.OverlapSphere colliders 1 Answer
object collision does not work 2 Answers
multiple colliders 1 Answer
how to stop mesh entering another mesh, both with colliders 3 Answers