A* Pathfinding Project

Pathfinding randomly not working


#1

Reddit post with video

For some reason, the pathfinding on my 2d game is not consistently working. The target, which is an empty game object, is correctly moved when I click, and the player always has this gameobject as its target.

Strangely, the player moves in the Z axis despite the target having a Z position of 0 (I noticed the player’s Z coordinate is based on where it is on the grid…??). I don’t know if this is related.

Why does it not calculate the path every time? This is frustrating.


#2

Hi

How do you calculate the path and which movement scripts are you using?


#3

The movement scripts I’m using are AI path and AI destination setter.

I calculate the path by:
-Click translates to world point
-Empty game object is positioned at the world point
-Empty game object is assigned as “target”

Thank you for responding so quickly


#4

The script will recalculate its path to the destination regularly based on the ‘repath rate’ field on teh AIPath script. You can also call ai.SearchPath() if you want to immediately recalculate the path.


#5

This is my code:

GameObject click;
Pathfinding.AIDestinationSetter destination;
Pathfinding.AIPath aiPath;

void Start()
{
    aiPath = GetComponent<Pathfinding.AIPath>();
    destination = GetComponent<Pathfinding.AIDestinationSetter>();
    click = GameObject.FindGameObjectWithTag("click");
}
    void Update()

{
mouseLoc = Camera.main.ScreenToWorldPoint(mouseLoc);
if (Input.GetMouseButtonDown(0))
{
Vector3 mouseLocNew = shootDirection;
mouseLocNew.z = 0;
clickLocation = mouseLocNew;
}
}
void FixedUpdate()
{
Move();
}
void Move()
{
click.transform.position = clickLocation;
Debug.Log(click.transform.position);
destination.target = click.transform;
}


#6

I just added the SearchPath() function after

destination.target = click.transform;

and i’m still having the same issue. Also, my current repath rate is .1 and “can search” is enabled


#7

Hey,

Your script is doing a lot of extra stuff that is really not necessary.
here is a little bit simplified version of your script:

private GameObject click;
private Camera mainCamera;

void Start () {
    click = GameObject.FindGameObjectWithTag ("click");

    mainCamera = Camera.main;
}

void Update () {
    if (Input.GetMouseButtonDown (0)) {
        Vector3 mousePos = Input.mousePosition;
        mousePos.z = 0;
        Vector3 mouseLocNew = mainCamera.ScreenToWorldPoint (mousePos);
        mouseLocNew.z = 0;

        click.transform.position = mouseLocNew;
    }
}

Make sure the click gameObject is the same as set in the destination setter.
You only need to update the position, the destination setter will update the agent’s destination for you.

See if this works for you, feel free to comment if you need any further help.

ps. I highly disencourage using functions like FindGameObjectWithTag it’s easier to just make your click gameObject public and assign it through the inspector. :slight_smile:


#9

Thanks for helping me out!

I just made it so both my player controller and destination setter get the target through the inspector, and the problem persists :confused: . It seems like the problem is that A* isn’t calculating a route on some of the clicks, since it sometimes won’t draw a green route.

Any other ideas? Do you need to see more of my project to figure out why it’s glitching?

Also, does it matter that the player is fluctuating in the Z axis when he moves? I don’t know why this is happening, since the “Click” gameobject always has a Z of zero… agh


#10

Sure, could you share a picture of your graph and player setup?
I re watched the video on a larger screen and it does seem like everything should work.
Is there any warnings about Astar not being able to find a node close to the destination ?


#11

Yooo…
Your comment about graph setup prompted me to look at my A* grid graphs… for some reason, I had 2 identical grid graphs created. When I removed the extra one, it started working again!
Thank you so much lmao