Transition from one path-finding grid to another

Viewed 30

for some context, I'm creating a simple game for a university project where there are multiple seperate "islands" that the player can walk to.

The player also has a dog companion that is using an A* pathfinding algorithm that randomly selects an area within a certain range of the player, checks if they can walk there and it's not too close to where the dog already is, and returns true or not.

The issue I'm coming across is whilst the code works somewhat perfectly on the first island, when transitioning to the second island, it appears that every path that the dog tries to take gets rejected and causes a stack overflow error.

I've had a look at the debug inspector within unity to make sure that the scripts are referencing the scripts that they depend on rather than the first islands scripts, but it all appears to be correct.

I'm hoping to get some fresh eyes on this as it's only been me working on this for a few days now.

The code the error is getting created on:

public Node NodeFromWorldPoint(Vector3 worldPosition)
{
    float percentX = (worldPosition.x - transform.position.x) / gridWorldSize.x + 0.5f - (nodeRadius / gridWorldSize.x);
    float percentY = (worldPosition.z - transform.position.z) / gridWorldSize.y + 0.5f - (nodeRadius / gridWorldSize.y);
    percentX = Mathf.Clamp01(percentX);
    percentY = Mathf.Clamp01(percentY);

    int x = Mathf.FloorToInt(Mathf.Min(gridSizeX * percentX, gridSizeX-1));
    int y = Mathf.FloorToInt(Mathf.Min(gridSizeY * percentY, gridSizeY - 1));
    return grid[x, y];
}

This is the code that generates a random location within a range and returns if it's valid or not:

public Vector3 GetRandomPointInBounds(Bounds bounds)
{
    desintationReached = false;
    Vector2 location;
    location = new Vector2(
        Random.Range(bounds.min.x, bounds.max.x),
        Random.Range(bounds.min.y, bounds.max.y)
        );


    if (CheckIfPointInCollider(location))
    {
        //Debug.DrawLine(transform.position, location,Color.green,20f);

        return new Vector3(location.x, location.y, 0);

    }
    else
    {
        Debug.DrawLine(transform.position, location, Color.red, 0.1f);
        //Debug.Log("Invalid Location:" + location);
        return GetRandomPointInBounds(bounds);
    }

}

public bool CheckIfPointInCollider(Vector2 location)
{
    Node targetNode = grid.NodeFromWorldPoint(new Vector3(location.x, location.y, 0));

    if (targetNode.walkable && !rangeToIgnore.OverlapPoint(location))
    {
        return true;
    }
    else
    {
        return false;
    }
}

Heres the code that transitions between the first grid object and the second:

public void Launch()
{
    // Increasing Current Island Count

    gm.IncreaseIsland();
    Debug.Log("Transitioning to island: " + gm.GetCurrentIsland());

    // Enabling & Disabling Monty, Player, Canoe Single and their depencies

    canoeAIO.transform.position = canoeSpawnPoints[0].transform.GetChild(currentIsland).transform.position;
    canoeAIO.SetActive(true);
    canoeAIO.GetComponent<CanoePaddle>().beached = false;
    canoeAIO.GetComponent<CanoePaddle>().canPaddle = true;
    canoe.SetActive(false);
    player.SetActive(false);
    monty.SetActive(false);
    layerManager.SetActive(false);
    interactionsManager.SetActive(false);
    pathwayCollider.SetActive(false);
    spritesManager.SetActive(true);

    //Setting all other pathfinding managers to inactive
    for (int i = 0; i < pathfindingManagers.Length; i++)
    {
        pathfindingManagers[i].SetActive(false);
    }

    // Activating the next islands pathfinding and clearing any existing path requests

    pathfindingManagers[gm.GetCurrentIsland()].SetActive(true);
    PathRequestManager.ClearRequests();
    monty.GetComponent<MontyStateActions>().StopAllCoroutines();
    montyStateVariables.grid = pathfindingManagers[gm.GetCurrentIsland()].GetComponent<MyGrid>();
    montyStateVariables.currentlyOnPath = false;


    //hide monty
    //hide canoe object
    //show canoe all in one at the new location
    cameraHandler.SwitchToCanoe();

}

(btw, Monty is the name of the dog :) )

I hope this is helpful, and if there is any more code/images you need I'll try and get them uploaded

0 Answers
Related