Procedural Maze Generation W/ Pathfinding

I have a weird obsession with procedural generation. Not just levels, but anything. I think it has something to do with my other obsession, math. Either way, I took the procedural maze generation library that I was working on and decided to see if I could get A* pathfinding up and running on it. After hours of complete failure I decided to see what current libraries are out there. I found Simply A*, which was built for the engine I am using. Works perfectly. I did have to do some modification due to not writing this library myself, but so far I’m happy with it. Due to how the path grid is generated, I had to move all of my procedural map generation into the Awake() function so it would fire first, then generate the grid in Start(). For the enemy movement I dumped all “path” objects into an array and pulled a random Vector3 transform from this array every 5 seconds and pushed to the movement script, which is just an extension of the Pathfinding class from Simply A*. If the enemy reaches it’s desired path point before 5 seconds is up, it will randomly pick another so there is constant movement. You can view my proof of concept below. Keep in mind this isn’t a playable game, just a PoC.

http://xertz.net/ProceduralPathfinding/ProcedualPathfinding.html

Enemy movement script:

using UnityEngine;
using System.Collections;

public class SearchPathAI : Pathfinding
{
    public int SearchesPerSecond;
    public float MinDistanceToPlayer = 0f;
    public float AISpeed = 20.0f;
    public float AIPivotPointYValueOverGround = 0.0f;

    private Transform _player;
  public GameObject randomPositionPoint;
  public GameObject[] allPaths;

    void Start()
    {
        GameObject player = GameObject.FindGameObjectWithTag("Player");
    allPaths = GameObject.FindGameObjectsWithTag("Ground");
    InvokeRepeating("RandomPosition", 1, 5);
        if (player != null)
        {
            _player = player.transform;
            StartCoroutine(FindPathTimer());
        }
        else
            Debug.Log("Error: No gameobject found with the tag Player!");
    }

    void FixedUpdate()
    {

        if (Path.Count > 0)
        {
            MoveSearchingAI();
        }
    if(Path.Count <= 0) {
      RandomPosition();
    }
    }

    //A test move function, can easily be replaced
    public void MoveSearchingAI()
    {
        
    if (Vector3.Distance(transform.position, randomPositionPoint.transform.position) > MinDistanceToPlayer)
        {
            transform.position = Vector3.MoveTowards(transform.position, Path[0] + new Vector3(0, AIPivotPointYValueOverGround, 0), Time.deltaTime * AISpeed);
            if (Vector3.Distance(transform.position, Path[0]) < 0.4F)
            {
                Path.RemoveAt(0);
            }
        }
    }

    IEnumerator FindPathTimer()
    {
        //FindPath(transform.position, _player.position);
    FindPath(transform.position, randomPositionPoint.transform.position);
    Debug.Log(randomPositionPoint.transform.position);
        yield return new WaitForSeconds((float)(1.0f / SearchesPerSecond));
        StartCoroutine(FindPathTimer());
    }

  void RandomPosition () {
    int index = Random.Range(0, allPaths.Length);
    randomPositionPoint = allPaths[index];
    StartCoroutine(FindPathTimer());
  }
}

facebooktwittergoogle_plusredditpinterestlinkedinmail