News Forums RAIN General Discussion and Troubleshooting Behavior Tree - problem with basics


This topic contains 9 replies, has 2 voices, and was last updated by  prime 9 months, 3 weeks ago.

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
  • #37538



    I’m new to RAIN with some programming experience. I am really excited about RAIN but am really struggling to make it work sometimes. I have followed all four tutorials from CodersExpo and gone through the sample project which can be downloaded from the RAIN Tutorial section. I am definitely missing something but have wasted 3 days already trying to make the below bahavior tree work.

    Now. For starters the problem that I have is I can’t get RAIN working with basic behaviors like timers for example. Yes…

    I have a scene where the AI gets spawned in the corridor then it is heading to a nav target at the entrance of the hall and then it is heading to capture the flag nav target at the center of the hall. When AI gets there it detects the player and fires at him in rounds.

    Here my problems start.

    After detecting player the behavior tree of the AI should shoot endlessly at the player in rounds, 5 shots each, 1 second btw each shot and one 3 seconds btw each round. In parallel it is rotating to face the player. The AI behavior tree goes like this.

    [BT] AI logic
    *[SEL] selector
    **[CON] AI in the corridor
    ***[PAR] Move and animate in paraller - this is working. When AI gets to the hall set the inTheHall = true.
    **[CON] isInTheHall == true.
    ***[SEQ] sequencer
    ****[PAR] Move and animate to the nav target ‘flag’ at the center of the hall. When AI gets there…
    ****[PAR] paraller
    *****[ANIM] Stop
    *****[DETECT] Detect player. repeat = forever
    *****[SEL] check if aspect visible
    ******[CON] visible.
    *******[B] Behavior tree binding it handles shooting in rounds /* XML of the below at the end of this post */
    ********[PAR] parallel
    *********[ANIM] face target = player aspect
    *********[SEQ] sequencer. Fire 5 shots then 3 seconds break.
    **********[ITE] count = 5 /* Fire 5 shots */
    ***********[CUST ACN] custom action to spawn the shot prefab. This has been tested in a separate standalone [BT].
    ***********[TIMER] shot interval set to 1 second. Return = success This node is being executed endlessly and returns RUNNING
    **********[TIMER] round interval set to 3 seconds. Return = success. Tree execution never gets here because of the above

    WHAT AM I DOING WRONG ??? Please help.

    Using: UNITY 5.0.0f4 & RAIN

    The XML of the first [BT] tree for getting to the flag:

    <behaviortree version="1.1" repeatuntil="" name="BDLogic" debugbreak="False"><selector usepriorities="False" repeatuntil="" 
    name="selector" debugbreak="False"><constraint repeatuntil="" priority="" name="Not in Hall" debugbreak="False" 
    constraint="!isInTheHall"><parallel tiebreaker="succeed" succeed="any" repeatuntil="" name="parallel" fail="any" 
    debugbreak="False"><move turnspeed="" repeatuntil="" name="Target - Hall Entrance" 
    movetarget="navigationtarget("HallEntrance")" movespeed="3" facetarget="" debugbreak="False" closeenoughdistance="" 
    closeenoughangle="" /><animate repeatuntil="" name="WalkAnim" debugbreak="False" animationstate="" /></parallel><expression 
    returnvalue="evaluate" repeatuntil="" name="isInTheHall = true" expression="isInTheHall = true" debugbreak="False" />
    </constraint><constraint repeatuntil="" priority="" name="In the Hall" debugbreak="False" constraint="isInTheHall == true">
    <sequencer usepriorities="False" repeatuntil="" name="sequencer" debugbreak="False"><parallel tiebreaker="succeed" 
    succeed="any" repeatuntil="" priority="" name="Take the Flag" fail="all" debugbreak="False"><move turnspeed="" repeatuntil="" 
    name="Target - Flag" movetarget="navigationtarget("Flag")" movespeed="3" facetarget=""Flag"" debugbreak="False" 
    closeenoughdistance="1" closeenoughangle="" /><animate repeatuntil="" name="WalkAnim" debugbreak="False" animationstate="" />
    </parallel><parallel tiebreaker="fail" succeed="all" repeatuntil="" priority="" name="parallel" fail="any" debugbreak="False">
    <animate repeatuntil="" name="Stop" debugbreak="False" animationstate="" /><detect sensor=""eyes"" repeatuntil="running" 
    name="Detect Trooper" matchtype="best" entityobjectvariable="varCloneTrooper" debugbreak="False" consistent="True" 
    aspectvariable="aspCloneTrooper" aspectobjectvariable="" aspect=""aCloneTrooper"" /><selector usepriorities="False" 
    repeatuntil="" name="selector" debugbreak="False"><constraint repeatuntil="" priority="" name="varCloneTrooper != null" 
    debugbreak="False" constraint="varCloneTrooper != null"><treebinding repeatuntil="" name="BDShoot" debugbreak="False" 
    binding="BDShoot" /></constraint></selector></parallel></sequencer></constraint></selector></behaviortree>

    The XML of the second [B] tree for shooting in rounds:

    <behaviortree version="1.1" repeatuntil="" name="BDShootRounds" debugbreak="False"><parallel tiebreaker="fail" succeed="any" 
    repeatuntil="" name="parallel" fail="all" debugbreak="False"><move turnspeed="" repeatuntil="" name="Face Trooper" 
    movetarget="" movespeed="" facetarget="aspCloneTrooper" debugbreak="False" closeenoughdistance="" closeenoughangle="" />
    <sequencer usepriorities="False" repeatuntil="" name="sequencer" debugbreak="False"><iterator repeatuntil="" priority="" 
    name="FireRound" debugbreak="False" count="5"><timer waitforsec="1" returnvalue="success" name="Shot Interval" 
    debugbreak="False" /></iterator><timer waitforsec="3" returnvalue="success" priority="" name="Round Interval" 
    debugbreak="False" /><action repeatuntil="" priority="" parametervalues="MC41ADIANQ==" 
    parameters="U2hvdEludGVydmFsAFJvdW5kSW50ZXJ2YWwATWF4U2hvdHNQZXJSb3VuZA==" namespace="" name="Shoot Action" debugbreak="False" 
    classname="AIBDShoot" /></sequencer></parallel></behaviortree>
    • This topic was modified 9 months, 3 weeks ago by  Olin.
    • This topic was modified 9 months, 3 weeks ago by  Olin.
    • This topic was modified 9 months, 3 weeks ago by  prime.
    • This topic was modified 9 months, 3 weeks ago by  prime.
    • This topic was modified 9 months, 3 weeks ago by  prime.
    • This topic was modified 9 months, 3 weeks ago by  prime.


    It’s hard to tell what’s going on, at least partly because it looks like your BT is failing on animate nodes.

    For example, your Stop animate node doesn’t specify an animation state. That node will fail when it executes, which will cause its entire parallel to fail, which will cause the tree to restart execution. The result may be that it looks like your timer is executing forever - because each frame the timer starts and returns running, and then restarts on the next frame when its parent parallel restarts.

    Whenever we see “timers aren’t working” issues it is pretty much always a sign that the behavior tree is failing somewhere outside of the timer and that the timer is being restarted.

    Also - make sure you change all of your assignment expressions (expressions with content like “x = y”) so that their return value is Success, not Evaluate. (Your “isInTheHall = true” is one such case.)



    Hi Prime,

    Not expected that you would get back so quickly. Thanks !

    I think we are on the right track to solve this. I have removed all animations and the tree behaves differently now.

    Before (with failing animations) the [PAR] node was failing as well since the animation was failing. Now the tree is stuck on the [DETECT] which is in the REPEATING state which in turn probably makes the timer get stuck in the RUNNING state as well.

    So it goes like this starting from DETECT:
    [DETECT] - Repeating
    then all green until
    [SEQ] - running
    [ITE] - running
    [CUSTOM ACTION] - “Shoot action”. This fired once. Returned SUCCESS.
    [TIMER] - “Shot Interval”. Stuck in the RUNNING state.
    [TIMER] - Round Interval - never got there.

    How to get through this ?

    Adding main [BD] XML after changes:

    <behaviortree version=”1.1″ repeatuntil=”” name=”BDLogic” debugbreak=”False”><selector usepriorities=”False” repeatuntil=”” name=”selector” debugbreak=”False”><constraint repeatuntil=”” priority=”” name=”Not in Hall” debugbreak=”False” constraint=”!isInTheHall”><parallel tiebreaker=”succeed” succeed=”any” repeatuntil=”” name=”parallel” fail=”any” debugbreak=”False”><move turnspeed=”” repeatuntil=”” name=”Target - Hall Entrance” movetarget=”navigationtarget("HallEntrance")” movespeed=”3″ facetarget=”” debugbreak=”False” closeenoughdistance=”” closeenoughangle=”” /></parallel><expression returnvalue=”success” repeatuntil=”” name=”isInTheHall = true” expression=”isInTheHall = true” debugbreak=”False” /></constraint><constraint repeatuntil=”” priority=”” name=”In the Hall” debugbreak=”False” constraint=”isInTheHall == true”><sequencer usepriorities=”False” repeatuntil=”” name=”sequencer” debugbreak=”False”><parallel tiebreaker=”succeed” succeed=”any” repeatuntil=”” priority=”” name=”Take the Flag” fail=”all” debugbreak=”False”><move turnspeed=”” repeatuntil=”” name=”Target - Flag” movetarget=”navigationtarget("Flag")” movespeed=”3″ facetarget=”"Flag"” debugbreak=”False” closeenoughdistance=”1″ closeenoughangle=”” /></parallel><parallel tiebreaker=”fail” succeed=”all” repeatuntil=”” priority=”” name=”parallel” fail=”any” debugbreak=”False”><detect sensor=”"eyes"” repeatuntil=”running” name=”Detect Trooper” matchtype=”best” entityobjectvariable=”varCloneTrooper” debugbreak=”False” consistent=”True” aspectvariable=”aspCloneTrooper” aspectobjectvariable=”” aspect=”"aCloneTrooper"” /><selector usepriorities=”False” repeatuntil=”” name=”selector” debugbreak=”False”><constraint repeatuntil=”” priority=”” name=”varCloneTrooper != null” debugbreak=”False” constraint=”varCloneTrooper != null”><treebinding repeatuntil=”” name=”BDShoot” debugbreak=”False” binding=”BDShoot” /></constraint></selector></parallel></sequencer></constraint></selector></behaviortree>

    Adding bound [B] tree XML responsible for shooting:

    <behaviortree version=”1.1″ repeatuntil=”” name=”BDShootRounds” debugbreak=”False”><parallel tiebreaker=”fail” succeed=”any” repeatuntil=”” name=”parallel” fail=”all” debugbreak=”False”><move turnspeed=”” repeatuntil=”” name=”Face Trooper” movetarget=”” movespeed=”” facetarget=”aspCloneTrooper” debugbreak=”False” closeenoughdistance=”” closeenoughangle=”” /><sequencer usepriorities=”False” repeatuntil=”” name=”sequencer” debugbreak=”False”><iterator repeatuntil=”” priority=”” name=”FireRound” debugbreak=”False” count=”5″><action repeatuntil=”” parametervalues=”MC41ADIANQ==” parameters=”U2hvdEludGVydmFsAFJvdW5kSW50ZXJ2YWwATWF4U2hvdHNQZXJSb3VuZA==” namespace=”” name=”Shoot Action” debugbreak=”False” classname=”AIBDShoot” /><timer waitforsec=”1″ returnvalue=”success” name=”Shot Interval” debugbreak=”False” /></iterator><timer waitforsec=”3″ returnvalue=”success” priority=”” name=”Round Interval” debugbreak=”False” /></sequencer></parallel></behaviortree>

    • This reply was modified 9 months, 3 weeks ago by  Olin.
    • This reply was modified 9 months, 3 weeks ago by  Olin.
    • This reply was modified 9 months, 3 weeks ago by  Olin.
    • This reply was modified 9 months, 3 weeks ago by  Olin.


    In your BDShootRounds tree your parallel is set to succeed on any. As soon as the Face Trooper finishes (which probably happens faster than your timer) the parallel will quit and you may get the same effect as before.



    Hi Prime,

    First - thanks a lot for your support. Your advises helped but I still have couple of things that are not clear.

    I made the AI shoot forever by setting repeat=forever for Face Trooper mover and the following sequencer.

    Q1. Is this the only way to make shooting run forever ? I was experimenting but only this has worked.

    Q2. Getting back to your first advise on failing STOP animation. I am struggling to understand. With the covering [PAR] repeat = never why would it ever repeat when the animation is failing ? I would expect it to execute the anim once and never again after it fails - execute anim node once, failed ? doesn’t matter since I am not repeating anymore. I would expect same behavior for [PAR] repeat = Until Failure with fail = any.

    Don’t get me wrong - I’m not trying to be picky, just want to understand the logic and get most of this great tool that you are providing.

    Thanks !



    Q1: Your shooting node is a custom action, right? If so, you can make it run forever either by setting repeat = forever, or by having the node return ActionResult.RUNNING instead of ActionResult.SUCCESS.

    Q2: Even though the parallel was not set to repeat, once it exited (because of the failure) the entire behavior tree finished running. On the next frame the behavior tree reset and restarted, which caused the parallel to run again, etc. In other words, the entire behavior tree was repeating.



    Thanks again. Will try Q1.

    On Q2. Does this apply to any other nodes ? Can I read about this somewhere in the tutorial section no to bother you ? A direct link would be appreciated.

    Getting back to the BT. I have problems with failing animations now. There are basically two animations: Walk and Idle. Both are failing when running but the animation plays correctly and the AI is moving from start to the Flag. Animations are failing however thus the [PAR] is failing etc. etc. The AI is fully rigged and basically this is a copy of my player character which is working perfectly. I can place animation nodes anywhere in the [BT] and the result is the same.

    I have added the AI MecanimAnimator states for Walking and Idle. Both states were initially tested on the Player character.

    I have skimmed couple threads but not sure if discussions there are applicable to RAIN 2.1.10.

    My questions on animations are:
    Q1. What may cause the animation to fail ? Is it about Root Motion ? Since they have root motion the accompanying mover speeds are = 0 and the AI still moves forward. I have turned Apply Root motion in the Animator Component off and it didn’t help. I’m coming from an assumption that correct states for animations are RUNNING and SUCCESS once finished when not looped in the FBX importer or in the [BT].

    Q2. The animations are imported from FBX coming from Blender. They are looped and baked into pose for Y and XZ. Does it matter ? Turned the loop time off but the animations are still failing in RAIN.

    Q3. When adding Animation State in the Inspector via the RAIN AI Rig Component I can specify only one start parameter. What if I have two parameters controlling my animation e.g. for Walk speed = 1 and additionally for running the run parm should be > 0.2. This is when I additionally press SHIFT while walking. How to configure this in the RAIN Animator ?




    Q1: There’s a bug in the MecanimAnimator, likely causing the problem. We were just discussing the fix internally today.
    Q2: No, doesn’t matter.
    Q3: see below

    Here’s the real answer for animation: Don’t use the MecanimAnimator. We’re in the process of reworking all of that, but the MecanimAnimator was an early attempt to merge legacy and mecanim animation styles - and it doesn’t work well.

    Instead, I suggest:
    1) For all locomotion animations, including idle, walk, run, etc., you should control those through the MecanimMotor. In the motor you can add parameters that are forwarded to your state machine. The simplest case is to forward the Speed parameter, which you would then use to transition from idle to walk or run. A common transition is idle->walk when Speed > 0.1 and walk->idle when Speed < 0.1 2) For all other animations, use triggers to activate them (in most cases). Then use the Set Mecanim Parameter node in the behavior tree to activate it.



    Hi Prime,

    The AI is working now with Set Mecanim Parameters and MecanimMotor ! Thanks for your tips as I am getting much more comfortable with RAIN comparing to when I have started last week and so this topic should be closed shortly

    My last questions.

    I have not added any parameters to the MecanimMotor itself as I’m not sure how would I use them with triggers. I imagine your pt.2 applies to running where additional mecanim parameter is set. A detailed example of how to “activate” or “use” triggers for running would be appreciated. FYI I am using one mecanim state with 2D blend tree to handle walking in all directions and another state to handle running forward. That’s it.

    I am experimenting with damp time for mecanim parameters now but without much success. For [MECANIM PARM] Walk=1 and Damp = 0.05 I never get to the full length animation. The forward root motion is getting to full speed but the animation never gets to full length walking movement. Similiar for stopping. When I set Walk = 0 with Damp = 0.05 my AI is wandering around the last nav target until animation stops. Any hints on how to blend the root motion and animation from walk to stop and vice versa would be welcome.

    And lastly you mentioned that in [PAR] node when any child node fails the whole tree is reset. Does this behavior apply to any other nodes ? For [SEQ] when any child node fails I would expect that it just exits the [SEQ] node and proceeds with consecutive nodes (as I actually did for the [PAR] with fail=any).

    And once again thanks for your tips so far. They helped a lot ! I really want to excercise my example until I understand it completely before going to the wide open.



    We’ll be putting out a Mecanim example inside the starter kit very soon, which should help with the Mecanim questions. As far as I can tell, there are no problems with damp time. I don’t quite understand your setup - are you controlling the Walk through a set parameter node? If so, you shouldn’t be. You should control that through the Speed parameter of the motor.

    (3) The entire tree doesn’t reset whenever any one node fails. It resets when the entire tree finishes, which is what happened in your case when the parallel node failed. There is nothing unique about parallels vs. sequencers, etc.

Viewing 10 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic.