AI doesn't stop attacking

CraigHubbard

New member
Hello, I posted this on Discord, but this is probably a better place for it:

I've set up a melee-based character with a very simple BT intended to make it opportunistically attack when the player comes within range. The thing is that I want ONE attack, but the attack loops endlessly. The only way to fix that problem is to set the attack sequence's Abort Type to Self, but then the attacks get interrupted and won't actually connect.

I'm using the demo animator with the katana, but I get the same result with the sword. I don't see anything different in the character or item setup and my BT is basically a limited subset of the demo BT, so I'm very confused why I'm getting this result...

Shouldn't the Start Stop Use fire once and then proceed to the next node? Any theories why it might be getting stuck?

I'm using 2020.3.0f1, First Person Controller 2.3.2, and Behavior Designer 1.6.8 with the UCC integration (plus movement and tactics add-ons).
 
It doesn't sound like your attack is ever completing. You should be able to copy the melee item action from the demo scene to get the correct settings, but you'll want to pay attention to the Use/Use Complete Event.
 
Thanks for the quick response!

I'm not 100 percent sure I understand what you mean by "melee item action". I'm interpreting that to be things like Use, Use Complete, etc. on the weapon item, which I've got set up identically to yours.

As a test, I applied my BT to the nolan agent and get the exact same result. I call the Start Stop use action in a sequence, but it gets stuck there and never reaches the Wait node, which I added because you had one in the demo BT for a brief cooldown.
 
Is the Use task still active? The Start Stop Use task will only stop when that ability is no longer active (if you are waiting for the use complete set on the task).
 
Yes, the Start Stop Use task stays active and the sequence doesn't reach the Wait task that follows it. It happens both with my character, which is set up with a katana, as well as the demo agent with the sword, copy/pasted directly from the demo scene but overridden with my external BT.

In your response to someone with a similar problem, you suspected that "one of the animator attack states isn't sending an event to indicate that the weapon has been used", but I couldn't find where those events are coming from by looking at the demo animator. But the melee setup is also a bit tricky to follow, so maybe I'm just looking in the wrong place...

I can see that the character is switching back and forth endlessly between Attack 1 Heavy From Idle and Attack 2 Heavy From Idle.

Any theories?
 
Yes, the Start Stop Use task stays active and the sequence doesn't reach the Wait task that follows it. It happens both with my character, which is set up with a katana, as well as the demo agent with the sword, copy/pasted directly from the demo scene but overridden with my external BT.
Sorry, I should have said Use ability instead of Use task. The Start Stop Use task will start the Use ability and it won't stop until the Use ability stops. I suspect the Use ability isn't stopping. Your stop type on the Use ability should be set the automatic.

In your response to someone with a similar problem, you suspected that "one of the animator attack states isn't sending an event to indicate that the weapon has been used", but I couldn't find where those events are coming from by looking at the demo animator. But the melee setup is also a bit tricky to follow, so maybe I'm just looking in the wrong place...
You can find the events on the animation import: https://docs.unity3d.com/Manual/AnimationEventsOnImportedClips.html
 
Thanks for the clarification! I tried setting Stop Type to automatic but no luck. The AI still keeps looping Start Stop Use.

I can see that the Use ability is alternating between showing (Active) and not showing it, but the Start Stop Use task doesn't advance.

The demo agent is set to manual, but I guess that's intentional?

Also thanks for the tip on where to find the events. I've only ever added events on transitions until now since I've only been animating simple things like doors, containers, etc., so that's where I expected to find them.

Anyway, it looks like the sword and katana animations have the correct events.
 
Hmm.. tricky tricky. Can you insert a breakpoint within StartStopUse.OnUpdate and determine why the task isn't ending? When the Use ability stops this piece of code should stop the task:

Code:
                for (int i = 0; i < m_UseAbility.UsableItems.Length; ++i) {
                    if (m_UseAbility.UsableItems[i] != null) {
                        return TaskStatus.Running;
                    }
                }

               // The trick is getting here.
                m_WaitForUse = false;
                m_WaitForStopUse = true;
 
Hello!

I have the same problem. I have a character with a MeleeWeapon (one hand sword) which attacks endlessly depending on StopUseAbilityDelay (1 by default): it keeps attacking when it is 0, otherwise attacks only once. In both cases it gets stuck in the first "TaskStatus.Running" of StartStopUse action (BehaviourDesign).

Following the code I see this piece of code called in AbilityStopped:
m_UsableItems[I] = null;[/I] m_UseCompleted = true; if (m_UseEvent != null) { Scheduler.Cancel(m_UseEvent); m_UseEvent = null; } ResetCanStopEvent(i);

"m_UsableItems" is always null when "ResetCanStopEvent(i)" is called, so inside of it:
// Melee weapons will not have a stop use delay so should not reset the event. if (m_UsableItems[slotID] != null && m_UsableItems[slotID].StopUseAbilityDelay == 0) { m_CanStopAbility[slotID] = true; return; } m_CanStopAbility[slotID] = m_StopType == AbilityStopType.Manual && !m_AIAgent; if (m_CanStopEvent[slotID] != null) { Scheduler.Cancel(m_CanStopEvent[slotID]); m_CanStopEvent[slotID] = null; }
"m_CanStopAbility[slotID] = true;" this line will never be reached, so it won't be true, therefore there is no way the ability will end. Surely I am missing something.
StopType (Use) is set as Automatic. WaitForAnimationEvent ticked in both UseEvent and UseCompleteEvent, with no combos (single sword hit).

Thanks.

----------------------------

EDIT: Further investigations have revealed that StartStopUse.OnUpdate() never reaches m_WaitForStopUse = true; because the condition needed would be m_UseAbility.IsActive && m_UseAbility.UsableItems[0] == null, and this condition won't be true because of that:
- Last call to StartStopUse.OnUpdate() before Use finishes: m_UseAbility.IsActive is true; m_UseAbility.UsableItems[0] is not null.
- First call to StartStopUse.OnUpdate() after that last call: m_UseAbility.IsActive is false; m_UseAbility.UsableItems[0] is null.
So it goes from the first TaskStatus.Running; to the last one (in the last "if" block) and the ability tries to start again.

m_UseAbility.IsActive and m_UseAbility.UsableItems[0] are set to false and null in the same call from the animation event OnAnimatorItemUseComplete, so m_WaitForStopUse won't be true. Should the second "if" block set m_WaitForStopUse to true if m_UseAbility.IsActive is false after being true? Am I missing something? Thanks again.
 
Last edited:
Hello!

I have the same problem. I have a character with a MeleeWeapon (one hand sword) which attacks endlessly depending on StopUseAbilityDelay (1 by default): it keeps attacking when it is 0, otherwise attacks only once. In both cases it gets stuck in the first "TaskStatus.Running" of StartStopUse action (BehaviourDesign).

Following the code I see this piece of code called in AbilityStopped:


"m_UsableItems" is always null when "ResetCanStopEvent(i)" is called, so inside of it:

"m_CanStopAbility[slotID] = true;" this line will never be reached, so it won't be true, therefore there is no way the ability will end. Surely I am missing something.
StopType (Use) is set as Automatic. WaitForAnimationEvent ticked in both UseEvent and UseCompleteEvent, with no combos (single sword hit).

Thanks.

----------------------------

EDIT: Further investigations have revealed that StartStopUse.OnUpdate() never reaches m_WaitForStopUse = true; because the condition needed would be m_UseAbility.IsActive && m_UseAbility.UsableItems[0] == null, and this condition won't be true because of that:
- Last call to StartStopUse.OnUpdate() before Use finishes: m_UseAbility.IsActive is true; m_UseAbility.UsableItems[0] is not null.
- First call to StartStopUse.OnUpdate() after that last call: m_UseAbility.IsActive is false; m_UseAbility.UsableItems[0] is null.
So it goes from the first TaskStatus.Running; to the last one (in the last "if" block) and the ability tries to start again.

m_UseAbility.IsActive and m_UseAbility.UsableItems[0] are set to false and null in the same call from the animation event OnAnimatorItemUseComplete, so m_WaitForStopUse won't be true. Should the second "if" block set m_WaitForStopUse to true if m_UseAbility.IsActive is false after being true? Am I missing something? Thanks again.
I have the same problem. And no matter what the value of StopUseAbilityDelay is, it keeps attacking, stuck in StartStopUse task, but the Use Ability in Locomotion seems normal(switch between Active/Deactive). Hope @Justin will look into your investigations.
 
Top