Search Unity

Behavior Designer - Behavior Trees for Everyone

Discussion in 'Assets and Asset Store' started by opsive, Feb 10, 2014.

  1. opsive

    opsive

    Joined:
    Mar 15, 2010
    Posts:
    5,128
    That's correct - that code is only run for the editor and the runtime doesn't need anything changes.
     
  2. XOA_Productions

    XOA_Productions

    Joined:
    Nov 27, 2016
    Posts:
    24
    Hey there!

    I'm currently using the Utility Selector with some modified Evaluators. The final project will have a lot of different things that the AI can do, so I would like to make use of externalbehaviors to keep it tidy.

    Also, the user can change objects that the AI uses, for example he could upgrade a fridge into a bigger one, thus requiring slightly different subtrees to use it.
    I'd like the fridge to actually "tell" the ai how to use it when the ai chooses that particular evaluator, in order to avoid using a lot of conditionals to select between different subtrees for different versions of an object.
    Thus, I'd like to load the version of the external behavior that the AI actually needs into the Behavior tree, execute it, and then unload it, in order to be ready to load a different version the next time, should it be necessary.

    Currently, the Behavior Tree Reference is completely replaced once the external behavior is loaded, so that's not what I'm looking for. It'd be better if it loaded the tree as a subtree, exectued that and then unloaded it.
    Is there any way to achieve that?
    The BehaviorTreeReference isn't documented that much, so I currently have no way to modify it easily.

    Regards
    -Ben
     
  3. opsive

    opsive

    Joined:
    Mar 15, 2010
    Posts:
    5,128
    You can start the tree, have the reference load the subtree, and then when you'd like to unload it you can disable the tree and load a new reference. You can do this by overriding the GetExternalBehaviors method. Take a look at this post and this post for some ideas on how to achieve that.
     
  4. XOA_Productions

    XOA_Productions

    Joined:
    Nov 27, 2016
    Posts:
    24
    Thanks a lot! The method of simply using another Behavior, setting it up with the right external behavior and starting it didn't even cross my mind somehow. That is much easier and does exactly what I need! :)

    Just a quick fyi, if a behavior tree has a Behavior Tree Reference who's external behavior array hasn't been populated, it somehow causes issues with the execution of the behavior tree. It either causes the whole tree to stop functioning, or it causes sequences to stop executing the next task after the first one has returned success (without any kind of warning or error messages):
    Unbenannt.png
    I'm aware that it's intended to hold external behaviors, but since I was trying to populate it at runtime, it caused some headaches. So I thought I should mention it just in case that this isn't intended, or should throw some type of warning/error. (The issue isn't caused by our own version of the evaluator, it also happens when using the standard evaluator in this configuration.)

    Regards
    -Ben
     
  5. Radu392

    Radu392

    Joined:
    Jan 6, 2016
    Posts:
    210
    Hello, I'm using the tactical pack along with the third person controller but I'm having trouble rotating an AI to shoot upwards or downwards using the normal 'Attack' task from the tactical pack. The integration doesn't support it out of the box, the AI can only fire at targets at the same y level as itself. However if my the target is on even a small hill, the AI will just walk towards the target to get on the same level. I tried using some rotation tasks but nothing worked and I'm thinking it's because the attack task is hardcoded into using the AI's pivot. Or most likely it's because the Attack task is trying to modify the rotation of the tcp AI while the CharacterIK (or god knows what other script on it) is preventing it from doing the rotation since that would mess it up visually. Any idea on how to fix this?
     
  6. wood333

    wood333

    Joined:
    May 9, 2015
    Posts:
    851
    Quick questions:

    1. Where do I find the integration packages?

    2. The A* Pathfinding Project integration is not discussed in the documentation.

    3. In your "Welcome To Behavior Designer" pane, under "Documentation," its "Browse" not "Browser" (auto-correct error looks like)
     
  7. opsive

    opsive

    Joined:
    Mar 15, 2010
    Posts:
    5,128
    Thanks - can you explain that screenshot scenario a little more? I'm trying to figure out how to reproduce it to see if it's behaving correctly.
     
  8. opsive

    opsive

    Joined:
    Mar 15, 2010
    Posts:
    5,128
    1) http://www.opsive.com/assets/BehaviorDesigner/samples.php
    2) The A* integration has a similar setup as the regular Movement Pack integrations so all that you need to do is use the A* tasks which can be downloaded from http://opsive.com/assets/BehaviorDesigner/Movement/integrations.php
    3) Thanks!
     
    wood333 likes this.
  9. opsive

    opsive

    Joined:
    Mar 15, 2010
    Posts:
    5,128
    Thanks for the heads up. Within NavMeshTacticalGroup.RotateTowards change:

    Code (csharp):
    1.  
    2.                 transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, navMeshAgent.angularSpeed * Time.deltaTime);
    3.  
    to:
    Code (csharp):
    1.  
    2.                 var eulerRotation = Quaternion.RotateTowards(transform.rotation, targetRotation, navMeshAgent.angularSpeed * Time.deltaTime).eulerAngles;
    3.                 eulerRotation.x = transform.eulerAngles.x;
    4.                 eulerRotation.z = transform.eulerAngles.z;
    5.                 transform.eulerAngles = eulerRotation;
    6.  
    This will restrict the rotation to the y axis. Since the Third Person Controller only supports up = Vector3.up it'll work, but when I'm working on the integration for version 2 I'll have a more robust way of handing this.
     
  10. wood333

    wood333

    Joined:
    May 9, 2015
    Posts:
    851
    Ah, now I see how you provide integrations, etc. Thank you very much. :) I notice that you have an integration for Pixel Crushers' Dialogue System and Love/Hate. Have you considered an integration with their Quest Machine. I think it would be a nice addition.
     
  11. opsive

    opsive

    Joined:
    Mar 15, 2010
    Posts:
    5,128
    wood333 and TonyLi like this.
  12. Mikekan13

    Mikekan13

    Joined:
    May 30, 2015
    Posts:
    90
    As far as I can tell can see object isn't working properly for me. It sees through walls no matter how I set the mask. I'm using unity 2018.1.1
     
  13. opsive

    opsive

    Joined:
    Mar 15, 2010
    Posts:
    5,128
    If you are using the object mask version make sure you leave the target object and tag fields empty. This will ensure the mask is used.

    If it still doesn't work do other detection types work? If not it's probably a problem with the cast between the agent and the target in which case slightly adjusting the y value of the offset/target offset will help.
     
  14. Mikekan13

    Mikekan13

    Joined:
    May 30, 2015
    Posts:
    90
    Is there a way to get it to work with a tag or target object?
     
  15. opsive

    opsive

    Joined:
    Mar 15, 2010
    Posts:
    5,128
    Yes - if you set either the target object or tag field then it'll use that. The Can See Object example within the Movement Pack demo scene uses the target object. I recommend this approach as it is the most efficient because the task doesn't need to look up any objects.
     
  16. NewMagic-Studio

    NewMagic-Studio

    Joined:
    Feb 25, 2015
    Posts:
    454
    I saw your webpage and tried the demo but i saw some problems, the enemies shot through other enemies and some times they fail moving to the player or start shooting. The problem is not just a max number of enemies surrounding but that the available space is known so that enemies can place empty positions depending on its size or the space they need to move weapons as for instance a warrior with a big sword might need more space without making his sword hit other enemies.
     
  17. Mikekan13

    Mikekan13

    Joined:
    May 30, 2015
    Posts:
    90
    When I set it to work with either a tag or a target it sees through walls. I haven't tried it with layers but that won't work for me anyways.
     
  18. opsive

    opsive

    Joined:
    Mar 15, 2010
    Posts:
    5,128
    Can you insert a breakpoint within CanSeeObject.OnUpdate and see where the task is returning true (Success) when it should be returning false (Failure)? Unfortunately there is a lot to the CanSeeObjects so there really isn't a better way to debug as it could be one of many things. Alternatively if you can reproduce the issue within the sample scene I should be able to help debug it.
     
  19. opsive

    opsive

    Joined:
    Mar 15, 2010
    Posts:
    5,128
    The sample uses OnCollisionEnter for the bullets and the collider doesn't precisely cover the triangle so that's probably why it appears that the bullets don't hit. You have a really good use case though for having differently sized objects so I've made a note of it for a future update.
     
  20. XOA_Productions

    XOA_Productions

    Joined:
    Nov 27, 2016
    Posts:
    24
    Sure. Basically having a BehaviorTreeReference without an ExternalBehavior assigned in your BehaviorTree will cause issues and either stop the BehaviorTree from working completely, or, if you do it in the Utility Selector demo and attach it to one of the Sequences, it will cause other Sequences to stop executing after the first child returns success - and cause the BehaviorTree to completely stop working again.
    In the picture, the setup is identical to the Utility Demo, except I'm using a modified Evaluator - which isn't the reason for the issues - and you can see a BehaviorTreeReference without an ExternalBehavior. The Sequence on the right executed the Seek task, and once that returned success it just stopped working, instead of executing the StartCooldown task.
    At that point, the BehaviorTree basically stays in this state until one stops play mode. It won't select any other Evaluators, the sequence will not go on to execute the StartCooldown task, it's completely frozen in a way.
    Remove the BehaviorTreeReference task and start play mode again and it will work fine and go on to execute StartCooldown.

    I understand that Behavior Tree References should have an ExternalBehavior assigned, but there are no warnings when this isn't the case, and in some cases, the user might want to assign the ExternalBehavior during runtime. It's fine if it stays that way, but a warning when having an 'empty' BehaviorTreeReference in your BehaviorTree would be cool, so one can identify the problem a bit faster.

    Edit: I added some text to the image to explain it a bit further since the resolution was quite low - my bad.
    behaviorTreeIssue.png

    Regards,
    Ben
     
    Last edited: Jun 30, 2018
  21. opsive

    opsive

    Joined:
    Mar 15, 2010
    Posts:
    5,128
    Thanks for the details! Yeah, an external tree reference is required so I'll have the error check display an error in the editor if one of the fields are left blank.
     
  22. NewMagic-Studio

    NewMagic-Studio

    Joined:
    Feb 25, 2015
    Posts:
    454
    Can any of your assets make that enemies can go through a place like a door where just can pass one by one and not get stuck?
     
  23. rasto61

    rasto61

    Joined:
    Nov 1, 2015
    Posts:
    352
    Well Behavior Designer can do that. BD just gives you the blocks to build from. It can do anything really, but you will have to make it do it. I dont think any ai asset does what you want out of the box.
    With BD you could do something like monitor how many agents enter the space before the door and then allow them to go 1 by 1 through the door and only allow the next 1 when the first passed.
     
    opsive likes this.
  24. opsive

    opsive

    Joined:
    Mar 15, 2010
    Posts:
    5,128
    Following up on @rasto61's post, in order to accomplish this you would need two things:

    - The task has to be aware that the agents will be going through the door so it can queue the agents.
    - Your agents would also likely need to use local avoidance provided by the pathfinding implementation to avoid nearby objects.

    The Queue task within the Movement Pack provides some of the functionality for the first one, but it doesn't really extend beyond just that single task. You could use the implementation provided by this task as an example for other tasks though. It is similar to this technique:

    https://gamedevelopment.tutsplus.com/tutorials/understanding-steering-behaviors-queue--gamedev-14365
     
  25. canis

    canis

    Joined:
    Oct 25, 2013
    Posts:
    79
    got a newbie question,
    my "Selector" node keep switching but I'm fail to tell what happen.
    anyone can help ?

    What I wanted to do :
    the zombie keep the "wander" state, until it "CanSeeObject", and stay in "Seek" state, until the player leave it's vision area.
    at the same time, I want to adjust the angle & distance on different state.

    here is the video for demo the problem that I'm facing right now.



    attachment for the zombie behavior setting.
     

    Attached Files:

  26. opsive

    opsive

    Joined:
    Mar 15, 2010
    Posts:
    5,128
    Since you are using a both conditional abort on the sequence task for the left branch the Can See Object task is alternating between success/failure. My guess is that the ground is getting in the way which causes Can See Object to fail - try to increase the offset/target offset y value to something like 0.1. The description of this video also has a package that you can download which has a similar setup that you can compare against:

     
  27. canis

    canis

    Joined:
    Oct 25, 2013
    Posts:
    79
    Thank you for your replied,
    in short : set that offset to 0.5f, didn't solve the issue.

    yes, I had watch that video, in fact I'm starting from that structure.
    and try to implement the feature I wanted and try to understand how BT work.
    the "Sequence" abort type used to be "LowerPriority"

    however by using that setup, the issue will become this :

    "After discover the player, the zombie will not leave the left branch, even the player leave the vision area"

    that's why I though setting the "Sequence" condition to "Both" can force "CanSeeObject" run and detect if player are in-range.

    ---------------------
    I watch it frame by frame, discover that the main issue is "Seek" will return fail, when player start moving.... .hm.... still thinking how to fix it.

    can you teach me how to implement something like this in BT ?
    Code (CSharp):
    1. if (condition01)
    2. {
    3.     while (condition01 || condition02)
    4.     {
    5.         // Seek
    6.     }
    7. }
    8. else
    9. {
    10.     // Wander
    11. }
     
    Last edited: Jul 7, 2018
  28. opsive

    opsive

    Joined:
    Mar 15, 2010
    Posts:
    5,128
    Your tree looks like it is setup properly, it's just that Can See Object is returning failure when it should be returning success (and is the reason why seek stops). There are a lot of conditions that can cause can see object to fail so in cases like that what I normally do is place a breakpoint within CanSeeObject.OnUpdate and see where it returns early. This will definitively say what is going wrong and why it is returning failure. If you tell me the line number of it returning early I can also explain the logic behind it.
     
  29. canis

    canis

    Joined:
    Oct 25, 2013
    Posts:
    79
    Thanks, I find the reason, it's not BT's problem, it's Unity's physic.
    • the key factor is my "Player" - entire gameObject is move by physic on fixedUpdate. (yes, no Update invoked)
    • the Rigidbody Interpolate = "Interpolate" (because that stupid camera jitters)
    so, when I set Rigidbody Interpolate = "none", the issue gone.

    you guys can locate same problem if you have the above setup.

    MovementUtility.cs > Line #128,
    The Physics.Linecast will hit nothing and return "false" over there.

    not sure how but can you guys give me some idea how to work around this ?
    e.g. make a threshold or buffer to allow the target disappear for few frame...... (err.....damn it.)

    recorded issue
     
    Last edited: Jul 7, 2018
  30. opsive

    opsive

    Joined:
    Mar 15, 2010
    Posts:
    5,128
    Ah, that makes sense. What you can do is have your behavior tree also run within FixedUpdate so it matches your character. You can do this by changing the Update Interval on the Behavior Manager to manual and then manually ticking it. Take a look at this page for more info:

    http://opsive.com/assets/BehaviorDesigner/documentation.php?id=52
     
  31. Radu392

    Radu392

    Joined:
    Jan 6, 2016
    Posts:
    210
    I saw a post from the creator back in 2017, saying they plan on implementing saving and loading. So when will BehaviorDesigner have integrated saving and loading? Currently, me saving variables manually is not that big of a deal, but then I realized that certain tasks also need to be saved, such as the 'wait' task or the position(s) where the logic is currently at in the tree and many other task related issues which are just a nightmare to save and load. In most cases, restarting a tree on load, even with the variables loaded properly, isn't good enough to get the correct saved logic, unless I fill my trees with a ton of checks, which is just not feasible for big trees that already work properly without these checks.
     
  32. opsive

    opsive

    Joined:
    Mar 15, 2010
    Posts:
    5,128
    We add features based on the number of requests that we get for a particular feature and so far we haven't gotten too many requests for save/load. The next update will be editor focused with things such as a find dialog. With that said I haven't forgotten about save/load and would like to implement it :) I have heard of some people creating their own base class and setting up save/load for their tree so it's definitely possible to do, but I just haven't had enough time to be able to do it yet. So to answer your question.. I have no ETA but it is still on my radar!
     
  33. ZingZhou

    ZingZhou

    Joined:
    Mar 29, 2015
    Posts:
    3
    hey,opsive
    when i restart unity,
    the global variable will none?
    upload_2018-7-10_9-32-37.png upload_2018-7-10_9-32-37.png
     
  34. opsive

    opsive

    Joined:
    Mar 15, 2010
    Posts:
    5,128
  35. Lyle2013

    Lyle2013

    Joined:
    Dec 31, 2012
    Posts:
    7
    hey,opsive
    I want to know if it is possible to run Task Log1 every frame no matter how long the Task wait keep.
     

    Attached Files:

  36. dl290485

    dl290485

    Joined:
    Feb 4, 2018
    Posts:
    160
    Can anyone direct me to a tutorial which covers how to make a system where conditions need to be met to switch in and out of a function? So basically to not just go down one path and be stuck there, and to instead run in perpetuity switching states back and forward.

    For example I want to make a recon AI which will:
    1) Search / Wander / Patrol (one of these) around until it finds the target
    2) Spot the target
    3) Pursue the target (<-- This is as far as I got, but I'm now stuck here in this state)
    4) If the target goes out of sight then the pursuit is ended and so loops back to step 1

    So basically the part I'm stuck on is that once the AI see's the target it magically knows where to go to chase it and will never lose the target. I need the player to be able to escape.

    After getting an understanding of how to do this, I would then like to make it a bit more advanced so the AI will move to the last known location of the target when it's out of sight, maybe wander in a small area around that for a timer, then switch back to it's original general wandering or what ever state it started in. This part though probably needs a lot more info so I'll start by asking for how to do the 4 steps above and ask again for this other stuff later if I need to.
     
  37. opsive

    opsive

    Joined:
    Mar 15, 2010
    Posts:
    5,128
    If you place a repeater above Log1 then it will keep running the Log1 task. You can then abort it with the interrupt/perform interruption tasks:

    BehaviorScreenshot.png

    The interruption could also be done with conditional aborts:

     
  38. opsive

    opsive

    Joined:
    Mar 15, 2010
    Posts:
    5,128
    The basic setup is similar to this video:



    Instead of the log task on the right you'd have Search/Wonder/Patrol. For a more advanced tree explanation take a look at this page:

    http://opsive.com/assets/DeathmatchAIKit/documentation.php?id=3
     
  39. WinterboltGames

    WinterboltGames

    Joined:
    Jul 27, 2016
    Posts:
    259
    I was looking at the asset store and found about behavior designer I was wondering if the UI is going to get more polish soon?

    And what about editor performance? since I will be using behavior designer with other editor tools like Odin inspector and serializer and puppet 3d.

    (I'm buying it anyway I'm not going to miss the deal of 80$ only for an amazing asset like this!)
     
  40. opsive

    opsive

    Joined:
    Mar 15, 2010
    Posts:
    5,128
    I do have plans on adding more editor-related tools (such as find/replace) but can you give more details on UI polish? A complete UI change probably won't happen until we do a version 2 (which hasn't been started yet), but it would be easy to change something minor.

    Behavior Designer has its own serializer/inspector which would make it more difficult to use Odin, though you could use Object Drawers for specific Odin UI functionality. I haven't used Puppet3D but that one looks like it would work with the reflection tasks or you could create your own tasks that interact with it.

    :) Thank you!
     
    WinterboltGames likes this.
  41. dl290485

    dl290485

    Joined:
    Feb 4, 2018
    Posts:
    160
    Correct me if I'm wrong, but is that video not missing step 4 of my question? The part where the AI stops following the target because it has lost sight of it.

    If it does, what part of it tells it to link back to the third step down of the tree? How does it not get stuck on the seek part on the fourth step down?
     
  42. opsive

    opsive

    Joined:
    Mar 15, 2010
    Posts:
    5,128
    The conditional abort will do the four step. If you use a both conditional abort then as soon as the target is no longer in sight it'll return failure with Can See Object and move onto the next branch. This video has a good demonstration of the different abort types:

     
    dl290485 likes this.
  43. WinterboltGames

    WinterboltGames

    Joined:
    Jul 27, 2016
    Posts:
    259
    I mean the look of the lists such as the tasks list and the variable editor they look kind of pre-unity 4 lists but the rest of the UI is great.

    one more thing, why didn't you reply to my question about editor performance, will having say 25 nodes drawn at once make the editor run at 1 fps?
     
  44. opsive

    opsive

    Joined:
    Mar 15, 2010
    Posts:
    5,128
    I do want to update the variable list to use a reorderable list but the first priority will be some of these more editor-related tools. Sorry I missed the performance question. 25 nodes is no problem.. I've seen trees with hundreds of tasks.
     
    WinterboltGames likes this.
  45. WinterboltGames

    WinterboltGames

    Joined:
    Jul 27, 2016
    Posts:
    259
    Thanks for the info sir!

    One little more question from your point of view, is the performance of BD on android solid?

    I plan to use it for my game which involves a lot of characters and other stuff using many BTs and they also use unity new NavMesh (via your movement pack).
     
  46. opsive

    opsive

    Joined:
    Mar 15, 2010
    Posts:
    5,128
    Traversing the tree is quick - generally you're going to run into other bottlenecks before the behavior tree execution is your bottleneck.
     
    WinterboltGames likes this.
  47. WinterboltGames

    WinterboltGames

    Joined:
    Jul 27, 2016
    Posts:
    259
    So, you are saying that BTs will not affect the performance of the game?

    Also, what about ECS support?
     
  48. opsive

    opsive

    Joined:
    Mar 15, 2010
    Posts:
    5,128
    Anything added to your scene will affect performance - it's just that the tree traversal is fast so there is minimal effect from that. ECS is still in preview mode so Behavior Designer won't support ECS until after it is stable.
     
    WinterboltGames likes this.
  49. WinterboltGames

    WinterboltGames

    Joined:
    Jul 27, 2016
    Posts:
    259
    Thanks for the info again sir! and good luck with the development of v2 ;)
     
    opsive likes this.
  50. dl290485

    dl290485

    Joined:
    Feb 4, 2018
    Posts:
    160
    Thank you.