Support Forum

Target seems to change midway

I have debugged quite a lot of what’s happening and it’s quite weird.
I have a “charge” skill where I get the target’s position and then I add my vector forward to ram through him. So far so good.

m_Data.Seeker.StartPath( p_Caster.CharObj.transform.position, targetPosition, Callback );

private void Callback( Path p )
        {
            if( p.error )
            {
                Debug.LogWarning( p.errorLog );
                CalculatePathAsync();
            }
            else
            {
                    for( int i = 0; i < p.path.Count; i++ )
                    {
                        Debug.Log( m_Cube );
                        m_Cube = GameObject.CreatePrimitive( PrimitiveType.Cube );
                        Destroy( m_Cube.GetComponent<Collider>() );
                        m_Cube.GetComponent<Renderer>().material.color = Color.red;
                        m_Cube.transform.position = ( Vector3 ) p.path[ i ].position;
                    }
                
                m_Agent.isStopped = false;
            }
        }

The “debug” Cubes get created as should and have the correct positions.

Now where the problems happens:
On my Update method I have:

var buffer = new List<Vector3>();
                m_Agent.GetRemainingPath(buffer, out bool stale);
                for (int i = 0; i < buffer.Count - 1; i++) {
                    Debug.DrawLine(buffer[i], buffer[i+1], Color.red);
                }
                Debug.Log( stale );

On the first Update after the callback, I get the line showing up correctly, with that I mean it’s going through all cubes that were created in a straight line . stale is always false yet after a few seconds the end vector becomes different ( even though buffer.Count is the same ) and it seems like the path gets trimmed halfway and that’s where the agent actually stops.

Frame before "trim": (20.2, 0.0, -25.8) (-4.2, 0.0, 9.0)
Frame after "trim":    (20.2, 0.0, -25.8) (4.4, 0.1, -3.2)
Buffer count is at both times 2.

I’ve noticed in my debug:

Debug.Log( m_Agent.remainingDistance  + " " +  m_Agent.endReachedDistance + " " + m_Agent.reachedEndOfPath );

remainingDistance jumps from 40 to 30 at some point even though all the other distances get reduces uniformingly by 1-2 each time.

For this test Im running 1 agent and I have turned his RVO off even though it doesn’t make any difference. Everything is happening without any obstacles or any other issues around the agent.

Could something automatic be going on from the engine? I am not changing anything else in my code.
EDIT: Could it be that the algorithm sees that the player is in the middle and that the path will get blocked so it trims it there? Is there a way to turn that off or should I just switch layers?

Hi

Are you using the RichAI movement script?
In that case you probably shouldn’t be calling seeker.StartPath manually. That’s used if you are creating your own movement script.

Instead I would suggest that you set the ai.destination property.
The RichAI script will automatically recalculate the path based on the ai.repathRate setting.

See https://arongranberg.com/astar/docs/iastarai.html#destination

I am using the RichAI yeah, the problem with just setting the destination ( which I used to do ) is that it doesn’t have a callback so it would cause a bug with m_Agent.reachedEndOfPath

I also used to use:

targetPosition = AstarPath.active.GetNearest( targetPosition, NNConstraint.Default ).position;

but I guess that’s automated in the destination?

reachedEndOfPath is indeed a bit annoying to use with asynchronous path requests.
The newer reachedDestination property was added to fix many of these issues. Maybe that will work better for you? https://arongranberg.com/astar/docs/iastarai.html#reachedDestination

Note also that ai.pathPending will return true while the agent recalculates its path.

1 Like

I stopped using that when you had told me that it wouldn’t check if the target was outside of the mesh? So for making sure I don’t have bugs for whatever reason I use the reachedEndOfPath instead ( Point outside of mesh )

Ah. Okay.

Then in your case I would recommend something similar to

IEnumerator Start () {
    ai.destination = somePoint;
    // Start to search for a path to the destination immediately
    // Note that the result may not become available until after a few frames
    // ai.pathPending will be true while the path is being calculated
    ai.SearchPath();
    // Wait until we know for sure that the agent has calculated a path to the destination we set above
    while (ai.pathPending || !ai.reachedEndOfPath) {
        yield return null;
    }
    // The agent has reached the destination now
}

Though not necessarily as a coroutine.

1 Like

Sure, just a clarification, I have been using the ai.destination on it’s own in many cases without calling the SearchPath and it seemed to work, should I call it each time?

Nevermind, I figured it all out, the issue was all that + the canSearch I hadn’t realized it was “autosearch”, it was making ai.pathPending false at random times and it was bypassing the checks. Thanks Aron, all good now, I ll probably disable all the canSearch in my project as I’m doing the checks myself

1 Like