ITraversalProvider causing Unity to crash entering/exiting play mode

I have been using a 2d layered grid, and wishing to add in a height penalty based on the unit, I started to play with ITraversalProvider but when I perform a check to see if the node would need to have a penalty applied it seems to crash unity when entering/exiting play mode. What is more strange is that if I remove “WithinSight()” and add it back it tends to work fine but when I load it up again the error is there. Any help would be greatly appreciated.

Currently the HeightPenalty is in the MonoBehaviour.

class HeightPenalty : ITraversalProvider
{
public Vector3 currentPosition, transformRight;
public float MaxfieldOfViewAngle, MinfieldOfViewAngle, facingDirection, maxDistance;

    bool WithinSight(Vector3 node)
    {
        Debug.Log("did we even check? " + currentPosition + " " + transformRight + " " + facingDirection);
        float distance = Vector3.Distance(node, currentPosition);
        Vector3 direction = node - currentPosition;
        float angleDifference = Vector3.Angle(direction, transformRight * facingDirection);
        // An object is within sight if the angle is less than field of view
        return angleDifference < MaxfieldOfViewAngle && angleDifference > MinfieldOfViewAngle && node.y > currentPosition.y && distance <= maxDistance;
    }
    public bool CanTraverse(Path path, GraphNode node)
    {
        return DefaultITraversalProvider.CanTraverse(path, node);
    }
    public uint GetTraversalCost(Path path, GraphNode node)
    {
        Vector3 nodePosition = (Vector3)node.position;
        uint penaltyBonus = 50;
        if (WithinSight(nodePosition))
        {
            uint cost = (DefaultITraversalProvider.GetTraversalCost(path, node)) + (penaltyBonus);
            Debug.Log("Penalty: " + cost);
            return cost;
        }
        else
        {
            Debug.Log("Outside View");
            // Use the default costs
            return DefaultITraversalProvider.GetTraversalCost(path, node);
        }
    }
}

The GetPath script:

if (Time.time > lastRepath && seekerCompoenet.IsDone())
{
Debug.Log(“Checking this is running in test”);
ABPath path = ABPath.Construct(transform.position, targetTransform.position, null);
traversalProvider = new HeightPenalty()
{
currentPosition = transform.position,
transformRight = transform.right,
maxDistance = penaltyRange,
MaxfieldOfViewAngle = maxAngle,
MinfieldOfViewAngle = minAngle,
facingDirection = facingDirection
};
path.traversalProvider = traversalProvider;

        lastRepath = Time.time + repathFrequency;
        // Start a new path to the targetPosition, call the the OnPathComplete function
        // when the path has been calculated (which may take a few frames depending on the complexity)
        seekerCompoenet.StartPath(path);
    }

Hi

What error are you getting?

Also, that Debug.Log inside the GetTraversalCost method will probably be veeery slow. It’s going to go through a lot of nodes.

1 Like

I never get to see the error, Unity just freezes indefinitely when exiting the play mode… but after removing the debug.log it has stopped doing this. It was going slower finding its path but it was still running so unsure why removing the debug.logs fixed the exiting play mode freeze I was getting.

but thank you… I am so annoyed at myself now haha.

1 Like