2D Pathfinding Top Down

Hello, I am new in Unity and I make a game based on this game (here is project file, tutorials and a game), but difference of this game with mine is that I added walls, so enemies are stupid and they don’t see them, so I decided to use A* Pathfinding Project, made everything as in this guide, but I couldn’t make and then find good script for enemy’s movements(all of them did not work correctly). So, I want to ask if you have idea how to make movement good or maybe you have some script for top down 2D game. Thank you for help.

Hi

Is your 2D game using the XY plane or the XZ plane?
Currently there are no included movement scripts for the XY plane.

Hi, when I started making game, I chose 2D project and as I understand plane is xy. May I change plane from xy to xz?

Ok. You use the built in unity 2D.
No, sorry. Right now there is no included movement script that works in the XZ plane. However following the get started tutorial you should be able to convert that to use the XY plane instead.

I am working on adding some 2D movement scripts.

[EDIT] In version 4 of the package the built in movement scripts work in 2D worlds as well. Take a look at https://arongranberg.com/astar/docs/pathfinding-2d.php

I have looked on another topics on this forum earlier and I found topic where you wrote

I am in the process of writing some 2D scripts. Here are the 2 scripts required for top-down movement.
http://pastebin.com/mVqA0fHu19
http://pastebin.com/pkN8vNRz10

or you can use this simpler script
http://pastebin.com/DvjRLQLV1

Can I use some of this scripts for top down game in xy plane? And if it is possible, will enemy move right to the player and rotate on corners, that movement of enemy will be perfect?

Hi

Yes, I think you can.
Here are the pastes again (they seem to have expired).
http://pastebin.com/enHVbev4
http://pastebin.com/5iRzeJix
http://pastebin.com/tAsRFKyF

[EDIT] In version 4 of the package the built in movement scripts work in 2D worlds as well. Take a look at https://arongranberg.com/astar/docs/pathfinding-2d.php

1 Like

I tried to use them and in my opinion, AI Simple Lepr works best of all, but enemies move backwards and only to one point, but I chose Targer as Player. On screenshot you can see how one enemy is already on that point and another one moves there, and then all another enemies also move there and stop there. Maybe I made something wrong in A* gameobject or for Enemy

Hi

It would help if the settings for AISimpleLerp were visible in that screenshot instead of the two scripts you did not use.

That they move backwards is just a convention thing. Most scripts in Unity assume that the Z axis is forwards. So you have to rotate your sprite so that it looks correct.

Oh, sorry for incorrect screenshot. Now it is correct.

Ok. So you are saying that they do not follow the player but just move to the first position they saw the player at?

No, they don’t, player starts the game on the middle of the screen but enemies move to another place and stay there. I tried another scripts, there enemies move worse, but green line of search(Gizmos) is also laid to the same point.

Huh. That’s odd.
Are you sure you have set it to the correct player object and that you haven’t set it to some other player object by mistake?

Also. I see that you are using a canvas for the game world. This is not ideal since the canvas uses screen coordinates and not world coordinates. I recommend that you use the unity Sprite system instead of the GUI system. Otherwise you will have problems when you want to move your camera around or zoom.

1 Like

Thank you very much for helping me. I think that I made it right. I used Canvas, because I found video tutorial with them, next time I will use Unity Sprite system. I make this game as big homework in university. If it is not hard, may you look on the project please. Maybe you will see some mistake.

Hi, I found a problem. I have prefabs of Player and Enemy and in Hierarchy I have spawners that makes clones of player and enemy and I added seeker and AI Simple Lepr to Enemy Prefab and then it did not work(that’s my poblem)., but then I just added Enemy and Player to Hierarchy and there Pathfinding system works good. So I found a problem, but I don’t understand the reason why it doesn’t works if make it with prefabs.

Hi

If you have specified the Player prefab as the target in the enemy prefab, it will try to follow that, not the cloned player unless you explicitly set that as the target. So what you need to to is to make sure that the target of the enemy is actually the spawned player and not the player prefab. If you click on the field in the unity inspector it will highlight what object it is linked to.