Pathfinding in a Grid-Based RPG... Theorycrafting and solution.

I am working on a Grid-Based title similar to Legend of Grimrock and/or Eye of the Beholder. In terms of pathfinding, I have gone through many issues as of lately.

My enemy has a wander AI which is very incomplete and only for testing purposes; I will implement a second-based AI later (this will be clear when you see the script below).

I tried using A*, but it merely causes the enemy to drag along, and not move in a grid. I had a script that rounded the current position to a whole number, which is the basic grid my game runs on. That worked to an extent; however, the enemy just dragged on without rotating at corners or going through his normal movement behavior.

Is there any way I can have the enemy move in a grid-based functionality, tracking down the player dynamically, and rotating at corners/rotating to face him?

Thanks in advance! :smiley:

Enemy movement script (with wander AI(As you can see, the FoundPlayer method is empty as I have no clue what to do :P)):

using UnityEngine;
using System.Collections;

public class EnemyMovement : MonoBehaviour {
	
	public static bool CanMoveForward;

	public bool FoundPlayer;

	//Translation:
	float movSpeed = 4.0f;
	Vector3 pos;
	Transform tr;
	public static bool moving = false;
	
	//Rotation:
	public static bool rotating = false;
	public float rotSpeed = 360f;
	float rotDegrees = 0f;
	Quaternion rotToAngle ;
	
	void Start () {  
		pos = transform.position;
		tr = transform;

		CanMoveForward = true;

		FoundPlayer = false;
	}

	void MoveTowardsPlayer() {
		return;
	}

	// Update is called once per frame
	void Update () {

		int randomValue = Random.Range (1, 100);

		Debug.DrawRay(transform.position, transform.forward, Color.red);

		if (!moving && !rotating && !FoundPlayer) {

			if (randomValue == (1 | 2) && tr.position == pos && CanMoveForward) {
				pos += transform.forward;
				moving=true;
			}

			else if (randomValue == 5 && tr.position == pos) {
				rotDegrees -= 90f;
				rotToAngle = Quaternion.Euler(0, rotDegrees, 0);
				rotating = true;
			}

			else if (randomValue == 6 && tr.position == pos) {
				rotDegrees += 90f;
				rotToAngle = Quaternion.Euler(0, rotDegrees, 0);
				rotating = true;
			}

		} else if (FoundPlayer) {
			MoveTowardsPlayer();
		}
		
		
		
		//Translation:
		if (moving) {
			if (Vector3.Distance(transform.position,pos) <0.05f){
				transform.position = pos;
				moving=false;
				//				print ("FINISHED MOVE");
			} else {
				transform.position = Vector3.MoveTowards(transform.position, pos, Time.deltaTime * movSpeed);
			}
		}
		
		//Rotation:
		if (rotating) {
			if (Quaternion.Angle(transform.rotation,rotToAngle) <10f) {
				transform.rotation = rotToAngle;
				rotating=false;
				//				print ("FINISHED TURN");
			} else {
				transform.rotation = Quaternion.RotateTowards(transform.rotation, rotToAngle, rotSpeed * Time.deltaTime);
			}
		}
	}
	
}

:smiley:

I recommend Aron Granberg’s Astar Pathfinding Project. It supports Grid Graphs and also runtime graph updating. It should be perfect for your needs.