SetDestination for Pursue in A* Pathfinding Project Problems

seanrich92

New member
Hi,

I have been having problems with using BD for a simple pursue task. So the situation is that whenever an object with a behavior tree does pursue or even a simple version of it [1], the latest object that runs this code bugs out. It just runs the code once and would spam path completed on the console logs and would try to pursue randomly 10-20 seconds later. However, upon spawning the same object, the one that bugged out previously suddenly runs the code correctly and the latest one will bug out. This happens with any number of objects, it is always the latest one that bugs out while the others suddenly works as long as they're not the latest ones. Another thing that also works is that whenever I press my left mouse button, the bugged object suddenly does the pursue task then stop after completing the path while the others do it continuously.

I've also tested this outside BD, just running the same code in an monobehaviour update loop using SetDestination and it works flawlessly without the same problems with BD. I also use SetDestination on other movement tasks which works well like seek or some modified version of it. Any way to debug or figure out what's happening here?

C#:
[1] Example code:


using UnityEngine;

namespace BehaviorDesigner.Runtime.Tasks.Movement.AstarPathfindingProject
{
    public class SimplePursue : IAstarAIMovement
    {
        [Tooltip("How far to predict the distance ahead of the target. Lower values indicate less distance should be predicated")]
        public SharedFloat targetDistPrediction = 20;
        [Tooltip("Multiplier for predicting the look ahead distance")]
        public SharedFloat targetDistPredictionMult = 20;

        public GameObject target;
        public override void OnStart()
        {
            base.OnStart();
            target = // gets the gameObject here
            targetPosition = target.transform.position;
        }

        // Pursue the destination. Return success once the agent has reached the destination.
        // Return running if the agent hasn't reached the destination yet
        public override TaskStatus OnUpdate()
        {
            SetDestination(Target());
            return TaskStatus.Running;
        }

        private Vector2 Target()
        {
            // Calculate the current distance to the target and the current speed
            Vector2 targetVector = new Vector2(target.transform.position.x, target.transform.position.y);
            Vector2 transformVector = new Vector2(transform.position.x, transform.position.y);
            var distance = (targetVector - transformVector).magnitude;
            var speed = Velocity().magnitude;
            
            float futurePrediction = 0;
            // Set the future prediction to max prediction if the speed is too small to give an accurate prediction
            if (speed <= distance / targetDistPrediction.Value)
            {
                futurePrediction = targetDistPrediction.Value;
            }
            else
            {
                futurePrediction = (distance / speed) * targetDistPredictionMult.Value;
            }

            // Predict the future by taking the velocity of the target and multiply it by the future prediction
            var prevTargetPosition = new Vector2(target.transform.position.x, target.transform.position.y);
            // targetPosition = targetVector;
            Vector2 movement = targetVector + (targetVector - prevTargetPosition) * futurePrediction;
            return movement;
        }
    }
}

Link to the video:
Pursue Example
 
Unfortunately this is really hard to debug without being able to reproduce it. Things such as the left mouse click sound like a red herring since the behavior tree doesn't detect input unless you are specifically using an input task. Your task looks correct though so you could ensure that the expected destination is set.
 
Hi Justin,

Thanks for the reply. Just now I tried changing the Path Logging options in A*, then suddenly it worked flawlessly with BD. Normally I have it on normal/heavy during production, however when I changed it to none/in-game, it just works. Just my speculation but it might have something to do with the latest spawned unit spamming too much in the logs. Might be something for others to take note if they use BD w/ A* pathfinding project.
 
Top