// Update is called once per frame

    void Update()
    {
        currentUnit = unitHandler.GetCurrentlyActiveTurnUnit();

        //only do this if it is the players turn
        if (currentUnit.GetFaction() == Faction.Player)
        {
            currentNavAgent = currentUnit.GetComponent <NavAgent>();
            if (currentNavAgent.GetIsMoving())
            {
                return;
            }

            startPosition       = currentUnit.transform.position;
            maxDistanceToSample = currentUnit.GetAvailableAP() + 2.0f;

            if (EventSystem.current.IsPointerOverGameObject())//Check to make sure we are not over a UI element.
            {
                return;
            }
            RaycastHit hit;
            Ray        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out hit, 100.0f, navLayerMask))
            {
                //Vector3 tempPos = hit.transform.position;
                Vector3 hitLocation = hit.point;
                //TODO: Do a distance check to make sure we don't sample too long a path.
                if (pathFinder.GetNodeFromGridPosition(hitLocation) != null)



                //if (hit.transform.GetComponent<Node>())
                {
                    targetPosition = pathFinder.GetNodeFromGridPosition(hitLocation).GetGridPosition();
                    UpdateDestinationCursor(targetPosition, true);
                    //Update the elevation controller with the ypos of the destination cursor.



                    if (endPosition != targetPosition)
                    {
                        linearDistanceToTarget = Vector3.Distance(startPosition, targetPosition);
                        if (linearDistanceToTarget < maxDistanceToSample)
                        {
                            endPosition = targetPosition;
                            RequestPath();
                        }
                        return;
                    }

                    if (Input.GetButtonDown("LeftClick"))
                    {
                        currentUnit.GetComponent <NavAgent>().MoveNavAgent(finalPath);
                    }
                }
                else if (!hit.transform.GetComponent <Node>())
                {
                    if (isDestinationCursorActive)
                    {
                        UpdateDestinationCursor(targetPosition, false);
                    }
                }
            }
        }
    }