- Home /
Enemy AI Movement one axis at a time (8 directional - 2.5d) towards player/target
I'm having real trouble with this, i have a character which uses .GetAxisRaw to move in 8 directions in a 2.5D environment, see pic:
The issue is, i don't think there is anyway i can use the same system(GetAxisRaw) for my Enemy AI movement? I have tried various scripts for other methods and they all seem to move the Enemy AI between x,y causing the Enemy to take the shortest distance to the target which looks like he is Iceskating in a 2.5d Environment.
What i need is a way to Lock movement to one axis/direction(of the 8) at a time while moving towards the player/target.
Thanks!
Answer by tomandresen · Jul 24, 2017 at 08:59 PM
You can take the position of the player as a Vector2 and subtract the position of your enemy, also as a Vector2.
You now have a direction vector towards the player. You can then either do something involving angles (and possibly the cross product between the vectors as Vector3s to determine direction), or you can simply inspect the x and y coordinates of the direction vector and use that to determine which of the 8 direction to move in.
Then, once you have determined that, you then move the enemy in the appropriate direction.
Answer by Lili-Shi · Jul 22, 2017 at 02:21 PM
I don't exactly know how to program AI-movement, but if you use any commonly used pathfinding systme (i.e. Dijkstra's Algorithm or A*), and a grid-based node-path, why don't set your AI up to only move along one of 8 nodes?
If you have no clue what I am talking about, donot fear. I highly recommend watching this video-series by quill18creates: Civilization/Dungeon Tile Movement & Pathfinding It is very good, and quill18 explain's the system behind that very well.
I have looked into A* and other navmesh type AI system's but I'm not really sure a grid system will work for the map sizes i intend to use and i don't really want to splash out $100 to find out it doesn't suit my needs.
The AI i intend to use is really simple, idle, chase once player enters within x range, and that's all there really is to it. Once i can work my movement issue it should be fairly straight forward.
Thanks for the info though.
Why spend any money for anything? Again, you can take a look into the video-series I linked, and take a look as to how to program it yourself. In that series quill18 creates a procedurally generated terrain, that consists of cubes (that that really is a grid-based node-system).
Also, I guess a grid-based node-system would be the exact thing to suit your needs, you want the AI to only move along 4 axis (N-S/S-W, W-E/E-W, NE-SW/SW-NE, NW-SE/SE-NW) and a grid would achieve exactly that (as you could set the AI up to move over the vertices and edges).