I’ve been working on a turn based top-down style game, and I’ve been able to get everything working except for actually implementing enemy AI, I looked for examples and tutorials online, but unfortunately all the examples were either too specific to the example project that I couldn’t use it for the game, or the AI wasn’t actually that good, some examples were just how to get the enemy to move in a straight line towards the player in a turn based way, which doesn’t make for good AI, right now I just would want the enemy to be able to use it’s position and the player’s position to calculate a path that would take the enemy the shortest way to the player, probably just through trying every possible path and picking the shortest one since I know it’s really difficult to write an algorithm that can perfectly find the shortest path first try. It would also have to check for obstacles, such as walls, other enemies or the player, so the enemy doesn’t just walk over that stuff. Would anyone know of good resources or a tutorial that handles this? or I guess if you know how to do this yourself that works too. Also, if you need to see any of my scripts, please let me know, I currently don’t have much of an enemy AI script which is why I didn’t post it.
Hello!
I’ve worked with a number of pathfinding algorithms, unfortunately Unity does not have a good implementation for 2D pathfinding. I first learnt about pathfinding by starting off with the basic algorithms and working my way up.
This resource goes over the Dijkstra’s Algorithm pretty well: Pathfinding Algorithms. Please read me | by Chet Chopra | The Startup | Medium. Once you understand how the initial algorithm works you can move on to A* which tends to be the most efficient at finding the shortest path.
For A* I am pretty partial to this explanation: A* Search Algorithm - GeeksforGeeks.
If you just need the algorithm and don’t want to do it yourself, there are loads of examples both paid and free of A* on the assets store.
Often rogue-likes top-down style only implement the basic follow the player because the player and enemies are normally very close when they meet, however you can calculate the best path by determining if a space is occupied by something else or not.
Hope this helps!
Thank you!, this does help a lot, I’ll be sure to try out that stuff, and I’ll let you know how it goes later
For sure! And of course, there are lots of different kinds of pathfinding algorithms. Do some exploring; it’s a neat topic!
Best of luck. ![]()
I got started on writing the script, although, I’ve run into a weird issue, I’m currently just writing the part where enemy moves based off RNG when they would be in corridors or rooms without the player, I haven’t gotten far with the script though because for some reason it will only call the chunk off the script that moves the enemy to side when the player moves
using UnityEngine;
public class EnemyAI : MonoBehaviour
{
public GameObject PlayerPosition;
Vector3 currentPosition;
Vector3 desiredPosition;
float CellSize = 1.0f;
float MoveSpeed = 5.0f;
Vector3 SpawnPoint;
void Start()
{
SpawnPoint = new Vector3(transform.position.x, transform.position.y, 0);
}
void ReadInput()
{
if (Input.GetKeyDown(KeyCode.DownArrow) || Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.RightArrow) || Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.W) | Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.D))
{
int x = 0;
int y = 0;
if (Random.value < 0.5f)
{
//we move to the side
if (Random.value < 0.5f)
{
// we move left
x = -1;
y = 0;
}
else
{
//we move right
x = 1;
y = 0;
}
}
else
{
//we move up or down
if (Random.value < 0.5f)
{
// we move down
y = -1;
x = 0;
}
else
{
//we move up
y = 1;
x = 0;
}
}
if (x < -0.5f)
{
desiredPosition += Vector3.left * CellSize;
}
if (x > 0.5f)
{
desiredPosition += Vector3.right * CellSize;
}
if (y > 0.5f)
{
desiredPosition += Vector3.forward * CellSize;
}
if (y < -0.5f)
{
desiredPosition += Vector3.back * CellSize;
}
}
}
bool Moving()
{
// move
currentPosition = Vector3.MoveTowards(currentPosition, desiredPosition, MoveSpeed * Time.deltaTime);
// rotate
//currentRotation = Mathf.MoveTowardsAngle(currentRotation, desiredRotation, RotationSpeed * Time.deltaTime);
// done?
Vector3 d = currentPosition - desiredPosition;
return d.magnitude > 0.01f;
}
void DriveTransform()
{
transform.position = currentPosition + new Vector3(SpawnPoint.x, SpawnPoint.y, 0);
}
void Update()
{
if (!Moving())
{
//if (!IsAttacking)
//{
ReadInput();
//}
}
DriveTransform();
}
}
right now the goal of this script is to have either up, down, left or right to be picked randomly and the enemy just moves that way, I haven’t added checks for collision or the player yet since I’m just trying to get this part to work, do you have any suggetions for how to get this to work?
It only moved when the player moves because of this line if (Input.GetKeyDown(KeyCode.DownArrow) || Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.RightArrow) || Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.W) | Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.D))
You are checking for input from the player and running if the player input an action resulting in the enemy moving at the same time. Sound like you need some kind of turn system. The official unity tutorial on roguelikes implements a pretty good system: Unit Mechanics - Unity Learn That link should take you directly to unit mechanics which is the part it talks about turn based movement I believe.
Here’s some timeless stuff on Roguelikes:
https://journal.stuffwithstuff.com/category/roguelike/
Specifically this article:
https://journal.stuffwithstuff.com/2014/07/15/a-turn-based-game-loop/
I agree with that content! They also wrote game programming patterns. Very intelligent person.
Thanks everyone, I’ll try that