Moving a Enemy 1 space at a time using NavAgent as the guide

Hello,

Im having issues working this out, I have the enemy and player moving at 1 unit at a time. The enemy moves randomly around a maze which works perfectly as i dont need to go to a specific place.

When the Enemy has LOS of the player it must head towards it but still at 1 unit a time. The issue is working out what direction to go next so i tried using the nav agent with so many issues i reverted back to doing it from scratch using the CalculatePath function and trying to go to the next node.

Here is the code for enemy movement its split in to two base movement and EnemyMovement.

Base:

using UnityEngine;
using System.Collections;

[RequireComponent(typeof(CharacterController))]
public class BaseMovement : MonoBehaviour {

    public float turnAmount = 90.0f;

    private CharacterController Controller;
   
    void Start()
    {
        Controller = GetComponent<CharacterController>();
    }

    public void MoveForward()
    {
        Vector3 fwd = gameObject.transform.TransformDirection(Vector3.forward);
        RaycastHit hit;

        if (Physics.Raycast(gameObject.transform.position, fwd, out hit, 10.0f))
        {
            if(hit.transform.gameObject.name != "Wall")
            {
                transform.Translate(Vector3.forward * 2);
            }
        }
          
    }

    public void MoveBackward()
    {
        Vector3 fwd = gameObject.transform.TransformDirection(Vector3.forward);
        RaycastHit hit;
        if (Physics.Raycast(gameObject.transform.position, -fwd, out hit, 10.0f))
        {
            if (hit.transform.gameObject.name != "Wall")
            {
                transform.Translate(-Vector3.forward * 2);
            }
        }
       
    }

    public void RotateLeft()
    {
        transform.Rotate(0, -turnAmount, 0);
    }

    public void RotateRight()
    {
        transform.Rotate(0, turnAmount, 0);
    }
}

EnemyMovement

using UnityEngine;
using System.Collections;

public class EnemyMovement : BaseMovement
{
    public Transform target;
    public float distanceLeft;
    public int pathCornerCount;
    public GameObject NextWaypoint;
    public float fov = 60.0f;
    public LayerMask layerMask;
    private NavMeshAgent navAgent;

    private RaycastHit hit;
    private GameObject previousWaypoint;
    private NavMeshPath path;
    private float elapsed = 0.0f;
    private int nextCorner = 1;
    private bool isMovementInit = false;
    private bool isAttacking = false;

    void Start()
    {
        path = new NavMeshPath();
        elapsed = 0.0f;
        InvokeRepeating("MoveForward", Time.deltaTime, 1.0f);
        InvokeRepeating("NextMove", Time.deltaTime, 1.0f);
        navAgent = GetComponent<NavMeshAgent>();
        Debug.Log("Movement initialised!");
        isMovementInit = true;
    }

    // Update is called once per frame
    void Update()
    {
        //PlayerFoundMovement();  
        elapsed += Time.deltaTime;
        if (LineOfSight() && !isAttacking)
        {
            Debug.Log("Attacking Initialised!");
            CancelInvoke();
            InvokeRepeating("CalculatePath", Time.deltaTime, 1.0f);
            InvokeRepeating("PlayerFoundMovement", Time.deltaTime, 1.0f);
            isMovementInit = false;
            isAttacking = true;
        }
        else
        {
            Debug.Log("We are looking!");
            if (!isMovementInit && !isAttacking)
            {
                Debug.Log("Movement Reinitialised!");
                CancelInvoke();
                InvokeRepeating("MoveForward", Time.deltaTime, 1.0f);
                InvokeRepeating("NextMove", Time.deltaTime, 1.0f);
                isMovementInit = true;
            }
        }
    }

    public void NextMove()
    {
        float rand = Random.Range(0, 9);

        if (CheckLeft())
        {
            if (rand < 3)
            {
                RotateLeft();
            }
        }

        if (CheckRight())
        {
            if (rand < 6 && rand >= 3)
            {
                RotateRight();
            }
        }

        if (CheckFront())
        {
            RotateRight();
            RotateRight();
        }
    }

    public bool CheckLeft()
    {
        Vector3 right = gameObject.transform.TransformDirection(Vector3.right);
        RaycastHit hit;

        if (Physics.Raycast(gameObject.transform.position, right, out hit, 10.0f))
        {
            if (hit.transform.gameObject.name != "Wall")
            {
                return true;
            }
        }

        return false;
    }

    public bool CheckRight()
    {
        Vector3 right = gameObject.transform.TransformDirection(Vector3.right);
        RaycastHit hit;

        if (Physics.Raycast(gameObject.transform.position, -right, out hit, 10.0f))
        {
            if (hit.transform.gameObject.name != "Wall")
            {
                return true;
            }
        }

        return false;

    }

    public bool CheckFront()
    {
        Vector3 fwd = gameObject.transform.TransformDirection(Vector3.forward);
        RaycastHit hit;

        if (Physics.Raycast(gameObject.transform.position, fwd, out hit, 10.0f))
        {
            if (hit.transform.gameObject.name == "Wall")
            {
                return true;
            }
        }

        return false;

    }

    void CalculatePath()
    {
        if (!target)
        {
            target = GameObject.FindWithTag("MainCamera").transform;
            Debug.Log("Getting Target");
        }


        if (elapsed > 1.0f)
        {
            elapsed -= 1.0f;
            NavMesh.CalculatePath(transform.position, target.position, NavMesh.AllAreas, path);
        }

        // Update the way to the goal every second
        for (int i = 0; i < path.corners.Length - 1; i++)
        {
            Debug.DrawLine(path.corners[i], path.corners[i + 1], Color.red);
        }
    }

    public void PlayerFoundMovement()
    {
        if (target)
        {

            if (previousWaypoint != null)
            {
                Destroy(previousWaypoint.gameObject);
            }
           
            if (nextCorner < path.corners.Length)
            {
                float dist = Vector3.Distance(transform.position, path.corners[nextCorner]) - 2;
                previousWaypoint = GameObject.Instantiate(NextWaypoint);
                previousWaypoint.transform.position = path.corners[nextCorner];

                Debug.Log("Current Distance: " + dist);

                if (transform.InverseTransformPoint(target.transform.position).x > 0)
                {
                    var heading = target.position - transform.position;

                    float dot = Vector3.Dot(heading, transform.forward);
                    if(dot > 0)
                    {
                        Debug.Log("PlayerInfront");
                    }

                    if (dist <= 0)
                    {
                        Debug.Log("Turning Right");
                        MoveForward();
                        RotateRight();
                        nextCorner = 2;

                    }
                    else
                    {
                        Debug.Log("Moving Forward");
                        MoveForward();
                        nextCorner = 1;
                    }
                }
                else
                {

                    var heading = target.position - transform.position;

                    float dot = Vector3.Dot(heading, transform.forward);
                    if (dot > 0)
                    {
                        Debug.Log("PlayerInfront");
                    }

                    if (dist <= 0)
                    {
                        Debug.Log("Turning Left");
                        MoveForward();
                        RotateLeft();
                        nextCorner = 2;
                    }
                    else
                    {
                        Debug.Log("Moving Forward");
                        MoveForward();
                        nextCorner = 1;
                    }
                }
            }
            else
            {
                MoveForward();
            }
        }
    }

    public bool LineOfSight(){
        if(!target)
        {
            target = GameObject.FindGameObjectWithTag("MainCamera").transform;
        }

       
        Vector3 rayDirection = target.position - transform.position;

        if (Physics.Raycast(transform.position, rayDirection, out hit, Mathf.Infinity, layerMask))
        {
            Debug.Log("I hit: " + hit.transform.name);
            Debug.DrawLine(transform.position, target.position);
            if (hit.transform == target)
            {
                // enemy can see the player!
                //Debug.Log("See the player");
                return true;
            }
            else
            {
                // there is something obstructing the view
                //Debug.Log("Where is he?");
                return false;
            }
        }
        return false;
    }

}

all help is really appreciated

The enemy actually follows the player sometimes but it depends how in the maze the enemy sees the player, if he sees him just as he moves past a turning then the enemy carrys on past it then wont return to the turning, sometimes the enemy gets stuck rotating left and right because when rotated right the players to the left and when rotating back the players to the right