Struggling with path distance being infinite

  • A* version: [5.3.8]
  • Unity version: [6000.2.10f1]

Hello, I’m having an issue where when I try to move my character using mousePosition, it can only find a path a few tiles ahead. It does show the destination in Scene through the blue indication line, as well as the destination inside of FollowerEntity under Debug info, but under the same thing it shows remaining distance to be infinity. I can partially bypass this by holding my mouse down, starting close and then dragging it but of course this isn’t the intended purpose.

I’m using Recast Graph and Follower Entity as my game is 2D.
My Voxel Size is set to 0.07 using Tiles at size 16, and Character Radius is 0.14.

I also have it functioning normally using a basic Vector3 instead of mouse, no matter the distance.

Here is the code of the mouse version, as well as the basic Vector3 one that works fine.

public void PointAndClick()
{
if (Input.GetMouseButton(1))
{
    Vector3 worldPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    worldPos.z = aiReference.position.z; //Keep z

    var nearest = AstarPath.active.GetNearest(worldPos);
    aiReference.destination = nearest.position;

}
}
public void GoToInteractable(InteractableObject interactable)
{
Vector3 intendedSpot = interactable.transform.position;(Vector3)interactable.interactionOffset;
GraphNode nearestNode = AstarPath.active.GetNearest(intendedSpot).node;

Vector3 snappedSpot = (Vector3)nearestNode.position;
aiReference.destination = snappedSpot;
}

Any help to what is going wrong would be greatly appreciated. I have tried to increase Max Frame Time to test if it was a performance issue and it did not help.

Give this a try?

    Vector3 point = Input.mousePosition;
    point.z -= Camera.main.transform.position.z; // Set the z of the point to the z distance from the camera to 0
  
    Vector3 worldPos = Camera.main.ScreenToWorldPoint(point);

Looks like it was just ScreenToWorldPoint() not really anything Astar related. This got it working on my end.

1 Like

Thanks for the response teal,

Your code didn’t fix the issue but I’ve found out my problem today.
I had Recalculate Paths Automatically, on Follower Entity, put wrongly. I was under the impression I wouldn’t need to recalculate often and could instead only do it when something changes, but setting it to the default Dynamic with 2 seconds fixed my issue.

1 Like