Two tasks run simultaneously when it shouldn't

Tsidev

New member
Hello,
I'm quite new to the Behaviour Tree asset but I'm slowly getting the hang of it.

What my Behaviour Tree does (most of the time) / should do :
  • the agent wanders and, at the same time, checks if the player is within sight and if it is being hit (via a custom OnGettingHit event)
  • if the agent is hit or player is within sight, it will seek the player to go to its melee range.
  • once arrived at destination a "subtree" is executed : (it is where the problem lies)
    • agent is in combat task (only returns Task.Running) and at the same times, checks that its target is still within a certain distance
    • if its target gets too far away and is no more in melee range, it returns failure, inverted to success, so the Seek task can trigger and make the agent go to its target melee range again.
    • repeat this subtree.
It works most of the time. However, sometimes, the Seek task and the Combat task run simultaneously (it shouldn't). There is nothing in my combat task, only return Task.Running after a Debug.Log(). My Seek task however, alternates between "Has arrived" and "Set destination" even though the agent is already at melee range.

My Seek task is quite basic, based on the A* original one provided in the Movement Pack :
C#:
public class Seek : IAstarAIMovement
{
    [Tooltip("The GameObject that the agent is moving towards")]
    public SharedGameObject target;
    [Tooltip("If target is null then use the target position")]
    public SharedVector3 targetPosition;

    private EnemyManager enemyManager;

    public override void OnAwake()
    {
        base.OnAwake();

        enemyManager = GetComponent<EnemyManager>();

        if (enemyManager == null) Debug.LogError("Seek.cs > enemyManager is NULL !");
    }

    public override void OnStart()
    {
        base.OnStart();

        SetDestination(Target());
        enemyManager.enemyAnimatorManager.PlayRunAnimation();
    }

    // Seek 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()
    {
        Debug.Log("SEEKING task running");
        if (HasArrived())
        {
            Debug.Log("SEEK > HAS ARRIVED.");
            enemyManager.enemyAnimatorManager.PlayIdleBattleAnimation();
            return TaskStatus.Success;
        }

        Debug.Log("SEEK > SETTING DESTINATION.");
        SetDestination(Target());

        return TaskStatus.Running;
    }

    private Vector3 Target()
    {
        if (enemyManager.enemyCombatManager.target != null)
        {
            return enemyManager.enemyCombatManager.target.transform.position;
        }
        return targetPosition.Value;
    }

    public override void OnReset()
    {
        base.OnReset();
        target = null;
        targetPosition = Vector3.zero;
    }
}


When the bug occurs and the editor is playing, the Behaviour Tree looks like that (I guess it shouldn't look like that) :

Screenshot 2023-05-31 at 14.06.04.png


When I pause the editor, the Behaviour Tree change its appearance and looks like that (which should be how it looks) :

Screenshot 2023-05-31 at 14.05.44.png

While the bug occurs, the agent's target is still valid and the agent is already at melee range. The Seek task should return success and stop.
The Repeater has Count = 0, Repeat Forever = true and End On Failure = false.

The console looks like this (Combat task runs at the same time as Seek task and the Seek task alternates between SetDestination and HasArrived):
Screenshot 2023-05-31 at 14.18.05.png

I'm pulling my hair off on this. I can't find what's wrong..

Thanks
 
Oh my God.. I feel ashamed.

Right after my post a lightbulb appeared above my head and decided to check the Within Distance task.

It seemed that when the bug occurs, this task was always returning Failure because it concluded that the target wasn't within distance.

I had to set the Y offset to 1 instead of 0 which is the default value (in the Within Distance task).

I don't know if I should delete my post or not but I will let it be in case my Behaviour Tree can help someone, as other's BTs have helped me.

Thanks
 
Top