Sometimes I get offset target position by using AIPath

Hi,
I bought pathfinding and met a problem. It happened sometimes.
【AIBuilder(Clone) 】in red is correct target position … I’m using Behavior Designer 1.5.9 (and movement package 1.5.3), . …But behavior designer still using a* pathfinding to search path in code…

Could you please help on this ?

Thanks,
Ricky

Hi

You have Seeker -> Start End Modifier -> End Point set to ‘Snap To Node’. This means the target point will always be snapped to the center of the closest node.
It is easier to see where the different nodes are if you disable the ‘Show Connections’ checkbox in the Grid Graph settings. You will see that you have placed the ‘AIBuilder(Clone) target’ object at the corner of a node, not at the center of it. I think you will need to double the resolution (i.e halve the node size) of your graph.

Hi Aron,
Great! AI can reach correct target position now !

But there is another problem show up…
AI stop moving to target before it really arrived target position, I check the code from behavior designer . I found following function return FALSE. The root cause is that “aiPathAgent.TargetReached” called the aipath.TargetReached, which returned FALSE before AI reach target … Do you have any idea on this problem?

protected override bool HasArrived()
        {
            var arrived = HasPath() && aiPathAgent.TargetReached;
            if (arrived) {
                aiPathAgent.RemovePath();
            }
            return arrived;
 }

Thanks,
Ricky

Hi

You can lower the ‘End Reached Distance’ setting on the AIPath script to make it consider the target reached later.

This is really an issue with Behaviour Designer as the AIPath script normally continues moving to the precise location of the target even after the ‘Target Reached’ property has returned true.

Actually the target position is 64 units is distance from the AI. And I set the ‘End Reached Distance’ as 1 unit…

I can see the correct path was created and show up , but TargetReached return TRUE immediately before AI start to move to target position.

Hi Aron,
I have changed AIPath to AILerp , but the issue still existed. Following code is for your information
Do you think this problem is from Behavior Designer ?

 public void MoveToTarget(TileItem target,Action finish)
        {
            if(mTarget==target)
                return;

            mFinish = finish;
            mTarget = target;
            mAILerp.target = mTarget.transform;
            mAILerp.SearchPath();
        }

        private void Update()
        {
            if (Status == WorkerStatusEnum.RUNNING_TO_WORK || Status == WorkerStatusEnum.RUNNING_TO_HOME)
            {
                if (mAILerp.targetReached)
                {
                    if(mFinish!=null)
                        mFinish();
                }
                StatusText.text = "running";
           
        }

Thanks,
Ricky

Hi Aron,
I really need your help to fix this problem. Could you please advise on this?

Hi

The AILerp script definitely should reach the exact position of the end of the path. It never stops before the end of it.
Are you sure the path (green line in the scene view) ends at the right position?

Yes,I’m sure. The green line ends at the right position and disappear immediately,…

Weird.
Would it be possible for you to share your project/scene with me so I can take a look?

Sure,I will send to you ASAP.
Thank you for help!

Hi

I haven’t managed to replicate it yet, but looking at the scripts I think it might be due to how the behavior trees have been configured and how Behavior Designer works.
It looks like you have two behaviors, both a Wander behavior and a Build behavior. I think what might happen is that the Wander behavior makes the agent start to follow a path, but then that behavior is disabled, the Build behavior is enabled instead and starts a path request, but during the time which that path request is being calculated the agent reaches the end of the original wander path and the Behavior Designer script might think that this indicated that it had reached the end of the build path (which hasn’t been calculated yet).

As I said, I haven’t manged to replicate the issue yet, so I’m not sure if this is actually what is happening, and I don’t know behavior designer well enough to say for sure.
In any case, you could try this workaround:
Open the AIPathMovementAgent.cs script (part of Behavior Designer) and change the lines around line 44 to look like

 aiPathAgent.target.position = target;
 aiPathAgent.canMove = true;
 aiPathAgent.SearchPath();
 aiPathAgent.GetComponent<Seeker>().GetCurrentPath().BlockUntilCalculated();

This will make sure that the path is calculated immediately instead of being calculated in a separate thread.
It is slightly worse for performance, but at least for this stage in your game it should be fine.

Hi Aron,

Thank you very much ! I changed the code.
The problem don’t happen again right now! :slight_smile:

1 Like