Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Behavior Designer - Behavior Trees for Everyone

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

  1. acgleader

    acgleader

    Joined:
    Apr 17, 2013
    Posts:
    40
    Thank you very much, Love you
     
    opsive likes this.
  2. magique

    magique

    Joined:
    May 2, 2014
    Posts:
    4,030
    Justin, are you still planning to add the Utility AI task concept that we discussed a while back?
     
  3. opsive

    opsive

    Joined:
    Mar 15, 2010
    Posts:
    5,122
    Definitely, it is in and will be released with the update that is going to go out shortly before the Deathmatch AI Kit :)
     
    Tinjaw likes this.
  4. magique

    magique

    Joined:
    May 2, 2014
    Posts:
    4,030
    Awesome. I look forward to it.
     
  5. magique

    magique

    Joined:
    May 2, 2014
    Posts:
    4,030
    I can't find documentation for the Movement Pack tasks. All I see is the following page, but it doesn't have the details on the individual tasks.

    http://opsive.com/assets/BehaviorDesigner/Movement/

    I'm trying to use the Within Distance task, but not getting the desired result. I have an inverter above Within Distance under a sequence. If not WithinDistance then the next sequence item is Seek. So, I basically want my AI Agent to seek the player until within a certain distance. I set the Magnitude to 2 and am using the TargetOffset as the player's current position, which I fetch first in the sequence. I thought this would work, but it continues to seek the player and just runs into her and keeps trying to move forward.

    EDIT: I also checked the variable that I am using to store the player's position and it is definitely updated correctly to the current value.
     
  6. opsive

    opsive

    Joined:
    Mar 15, 2010
    Posts:
    5,122
  7. magique

    magique

    Joined:
    May 2, 2014
    Posts:
    4,030
  8. opsive

    opsive

    Joined:
    Mar 15, 2010
    Posts:
    5,122
    Within Distance isn't being reevaluated - you'll need to add a Self conditional abort to the parent Sequence task.
     
  9. magique

    magique

    Joined:
    May 2, 2014
    Posts:
    4,030
    I get strange results from that. Now, the !WithinDistance and the Seek seem to go off and on as the AI agent approaches the player. It stutters when it goes off and then walks normal for a bit then stutters, etc. And eventually it just gets up to the player anyway. It seems to eventually stop right on top of the player, but if the player moves a little bit then it seeks again even though it is well within the distance specified.
     
  10. opsive

    opsive

    Joined:
    Mar 15, 2010
    Posts:
    5,122
    It's tough to say without playing it but normally you don't need a Can See Object and Within Distance immediately next to each other because Can See Object takes into account the distance. I would break your tree up into smaller branches, get one branch completely working, and then add to it from there. This will allow you to see exactly how and why it is running the way it is.
     
  11. magique

    magique

    Joined:
    May 2, 2014
    Posts:
    4,030
    That makes no sense to me. How can the Can See Object account for Within Distance? I must be completely misunderstanding its purpose. For Can See Object there is a visual distance for when the agent can see something, but Within Distance should be a check to see if an agent is within a certain physical distance. For what I'm doing they have to be together. Here is the logic I'm trying to employ:

    Can the AI agent see the player? If yes, and if the agent is not within 1 unit of the player then Seek him. If yes, and the agent is within 1 unit then don't seek him (because he's already close enough to attack).
     
  12. opsive

    opsive

    Joined:
    Mar 15, 2010
    Posts:
    5,122
    You can set the max magnitude that an object can see - if the agent can see the target then it is within that magnitude so it's within distance (assuming the can see and within distance magnitudes are the same).

    That's very similar to what I did with the Third Person Controller sample project. In it I used both Can See Object and Within Distance but they weren't right next to each other so I could change the evaluation order. In the screenshots I split it up so can see and within distance aren't focused in the screenshot but you can see it in the overview screen:



    If you look at the Can See branch you'll see that Can See Object is reevaluating using a lower priority abort and then further down on the bottom right of the branch there is a Within Distance that is being evaluated using a self abort type. I explain how this works on this page - if you do a search for "Within Distance" on that page you'll see something similar to your goal.
     
  13. magique

    magique

    Joined:
    May 2, 2014
    Posts:
    4,030
    They aren't the same and these two tasks should not be related in any way.

    I see how you are doing this and I can make adjustments. I think the biggest confusion comes from these weirdly named aborts. The concept is strange and the naming doesn't make any sense to me. I look at a Sequence and I think that it's going to go through that sequence every single frame and that it will only process tasks if the previous task (to the left) returns success. But it seems that's not how it works at all.
     
  14. opsive

    opsive

    Joined:
    Mar 15, 2010
    Posts:
    5,122
    Can See Object is basically a beefed up version of Within Distance. If you look at the Can See Object code you'll see that one of the first things that it does is a within distance check.

    As an optimization the behavior trees do not execute from the root node every frame. Conditional Aborts are similar to Observer Aborts in Unreal Engine - their explanation here may help show the differences. This Behavior Designer video also talks specifically about Conditional Aborts with examples of the three abort types.
     
  15. magique

    magique

    Joined:
    May 2, 2014
    Posts:
    4,030
    I watched the video and it's still something that is tough to wrap my head around. It's definitely not an intuitive concept. You even admit in the video that the idea can be confusing. It is for me for sure. Anyway, I understand how to fix the issue and hopefully I'll start to grasp this concept more as I use it more.
     
  16. opsive

    opsive

    Joined:
    Mar 15, 2010
    Posts:
    5,122
    Conditional aborts definitely take the most to understand out of all of the concepts. Once they click though you'll be designing new trees in no time. I'd summarize the three abort types with the following:

    Lower Priority: will reevaluate when any task to the right of the current branch is active
    Self: will reevaluate when any task within the current branch is active
    Both: will reevaluate when any task to the right or within the current branch is active.
     
  17. chaneya

    chaneya

    Joined:
    Jan 12, 2010
    Posts:
    416
    A huge thanks for posting this. :) And thanks to magique for admitting his difficulty in understanding conditional aborts. This is the first time I have seen an explanation that makes sense to me that I think will stick in my brain. I use conditional aborts a lot but usually I just cycle through them until my tree works the way I want it to work. Now that I actually know how they work vs. just glad for the tree to be working, I need to go back and reevaluate my trees.

    Allan
     
    magique and opsive like this.
  18. opsive

    opsive

    Joined:
    Mar 15, 2010
    Posts:
    5,122
    Awesome. I just added that summarized version to the conditional aborts docs.
     
    99thmonkey likes this.
  19. EvilGremlin

    EvilGremlin

    Joined:
    Aug 12, 2016
    Posts:
    231
    Hey I didn't read through the whole thread, so I am sorry if this was asked.

    I am new to Unity as well as node-based visual scripting. I am not really clear on compatibility between programs like Behavior Designer and other AIs. Can I take a script from another AI (for example, the capacity to recognize and shoot at an explosive barrel) and easily import this to Behavior Designer just calling the script from the node? Or am I going to have to manually program that functionality in? (Which is fine, I just wanted to know.)

    I ask because I found an AI package which basically has the core combat AI I want. But I also want to have more direct control over the NPCs and the rapid deployment functionality of visual scripting that a program like this can offer. Thanks in advance for your reply.
     
  20. opsive

    opsive

    Joined:
    Mar 15, 2010
    Posts:
    5,122
    Thanks for taking a look at Behavior Designer!

    That's correct - you could create a new task which just calls the method from the other package. You don't need to add all of the logic to the actual task (unless of course you want to). This is actually how pretty much all of the integration tasks work.
     
    EvilGremlin likes this.
  21. EvilGremlin

    EvilGremlin

    Joined:
    Aug 12, 2016
    Posts:
    231
    Perfect. Thank you.
     
  22. opsive

    opsive

    Joined:
    Mar 15, 2010
    Posts:
    5,122
    Behavior Designer 1.5.7 and Behavior Designer - Movement Pack 1.5.2 have been released on the Asset Store. You can see the full Behavior Designer release notes here and the Movement Pack here.

    Some of the larger new features include:

    Utility Selector task

    The Utility Selector allows you to evaluate tasks using Utility Theory AI. You can read more about the task on this page.

    External Tree Editing
    ExternalBehavior.PNG In the past when your Behavior Tree component had an external tree selected the editor would go into View Only mode
    to prevent you from unknowingly modifying the wrong tree. As I was using Behavior Designer with the Deathmatch AI Kit I found this to be cumbersome so now the editor will allow you to modify the external tree while the Behavior Tree component is selected.​

    Follow Task

    The Movement Pack has a new task: Follow. The agent will follow a Transform without getting too close. ​
     
  23. magique

    magique

    Joined:
    May 2, 2014
    Posts:
    4,030
    Good news. I'm happy to see the Utility AI task in this version. I'll be upgrading and trying it all out soon. Looks like a lot of great new stuff.
     
  24. unicoea

    unicoea

    Joined:
    Feb 18, 2013
    Posts:
    60
    Hi! I have a question and a suggestion.
    The question is :
    How to variable mapping to a custom script's public variable?
    The suggestion is:
    When copy a composite which have action child, and clicked on green "+" on left top of this composite icon, can we copy all child and itself together?
    Like and support you always!

    Thank you,
    unicoea
     
  25. opsive

    opsive

    Joined:
    Mar 15, 2010
    Posts:
    5,122
    Do you mean a custom field type or just a custom script? The variable mapping drop down will find custom scripts, but if you want to use it with a custom field make sure you've created a SharedVariable of that field type.

    The green "+" shows the children so I don't want to add a copy functionality to that icon because it would be confusing. Having it under a right click menu though could work.
     
  26. unicoea

    unicoea

    Joined:
    Feb 18, 2013
    Posts:
    60
    Hi! opsive
    I mean this: For example i have a new script like this:
    public class NewBehaviourScript : MonoBehaviour {
    public int abc = 0;
    }
    Can i variable mapping to this "abc"?

    Thank you,
    unicoea
     
  27. opsive

    opsive

    Joined:
    Mar 15, 2010
    Posts:
    5,122
    Yes, you can. If you create an int variable within Behavior Designer it'll be able to map to abc.
     
  28. unicoea

    unicoea

    Joined:
    Feb 18, 2013
    Posts:
    60
    Hi!Opsive
    Yes i have tried, i create a empty gameobject, attach "NewBehaviourScript.cs" to it, then create an int variable wthin a behavior tree, can't find the "abc" variable for mapping.
    Few days before, i remember it can find "NewBehaviourScript.cs" , but can't show the "abc" variable for mapping.
    Now it can't find "NewBehaviourScript.cs".
    I use Behavior Designer v1.5.7 with unity5.4(64bit), i don't know if any step or something is wrong.

    Thank you,
    unicoea
     
  29. opsive

    opsive

    Joined:
    Mar 15, 2010
    Posts:
    5,122
    I just looked at your script again. You'll need to add a property to abc:

    Code (csharp):
    1.  
    2. public class NewBehaviourScript : MonoBehaviour {
    3. public int abc = 0;
    4. public int Abc { get { return abc; } }
    5. }
    6.  
    Once you do that then it'll show up in the dropdown. Properties can be accessed a lot quicker than fields so that's why it only shows properties.
     
  30. TommiH

    TommiH

    Joined:
    Jan 14, 2008
    Posts:
    253
    Hello,

    Is there any way to call SetDirty on the variables on an external behavior tree? I'd like to write an editor script that automatically assigns values to them. I tried calling SetDirty on the whole ExternalBehaviorTree script but the values I set in my editor script didn't survive reopening the project (although they were reflected in the behavior tree asset's own inspector).
     
  31. opsive

    opsive

    Joined:
    Mar 15, 2010
    Posts:
    5,122
    The issue is that the tree isn't being reserialized. You'll want to call:

    Code (csharp):
    1.  
    2.   if (BehaviorDesignerPreferences.GetBool(BDPreferences.BinarySerialization)) {
    3.   BinarySerialization.Save(externalBehavior.BehaviorSource);
    4.   } else {
    5.   JSONSerialization.Save(externalBehavior.BehaviorSource);
    6.   }
    7.  
    Making sure you've included the BehaviorDesignerEditor.
     
  32. TommiH

    TommiH

    Joined:
    Jan 14, 2008
    Posts:
    253
    Thanks! That works.
     
  33. TommiH

    TommiH

    Joined:
    Jan 14, 2008
    Posts:
    253
    Another issue that's happened to me recently:

    The Behavior Designer drawing area seems to have become larger than the window in which it sits. See screenshot. As you can see, the scrollbar of the task list disappears at the bottom, the scrollbars of the tree area are not visible, and there's nothing visible of my tree (the start node was in fact placed slightly to the right of the visible area). Is there some way to reset this? My screen isn't big enough to increase the window size to match this..
     

    Attached Files:

  34. opsive

    opsive

    Joined:
    Mar 15, 2010
    Posts:
    5,122
    Hmm.. I haven't seen that happen before. Does it happen all of the time? It looks like you have a window layout error - have you tried resetting your window layout in the top right of the Unity editor?
     
  35. TommiH

    TommiH

    Joined:
    Jan 14, 2008
    Posts:
    253
    I tried reverting to factory settings, but this problem still persists. (I closed the BD window before doing that and reopened it.) Reopening Unity doesn't reset this either, nor does switching behavior trees.

    If by window layout error you mean the "Failed to destroy views #1", I hadn't noticed that and don't know what it's about. But I don't get that error every time this happens.
     
  36. opsive

    opsive

    Joined:
    Mar 15, 2010
    Posts:
    5,122
    Has it always done this or just recently? What if you create a new project? Or remove all of your editor settings with EditorPrefs.DeleteAll?
     
  37. TommiH

    TommiH

    Joined:
    Jan 14, 2008
    Posts:
    253
    It didn't do this at the beginning. I did notice a while ago that the contents seemed to be misaligned and that I always needed to use the zoom function back and forth to move around instead of using scrollbars. So it must have been happening for a couple of weeks at least.

    I tried EditorPrefs.DeleteAll and also a new project, and the problem still persisted in both cases... It doesn't happen with my colleague, but even if he sends me a project that just worked on his end, this issue happens on my end. Which makes me think it's got something to do with my screen size or something else specific to my Macbook Pro (15 inch, Retina)

    I also noticed that if I make the window smaller, the scrollbar next to the tasks gets smaller too (see screenshot). So it's as if resizing the window does affect the size of the drawing area, but somehow it's always drawn at double size or something. Could it be a retina thing...? (Though I've always been using this Macbook, even when this did work.)

    How does the system determine the size of the window?
     

    Attached Files:

  38. opsive

    opsive

    Joined:
    Mar 15, 2010
    Posts:
    5,122
    It does sound like it's a screen sizing issue. The scrollbars are positioned with Screen.width and Screen.Height minus an offset for the toolbar. If you output these values to the console are they correct? Since this could take some back and forth to get working, can you send me a PM with your Behavior Designer invoice number? That'll help reduce the noise in this thread.
     
  39. DCrosby

    DCrosby

    Joined:
    Jan 13, 2013
    Posts:
    86
    Wow 41 Pages, I read about 10 Before I started a search for Behavior Designer and Flight, and found nothing. A lot of AI engines, don't allow for flight because they're using a nav grid.And A* doesn't always mean it tracks things in elevation. So quick question, before I plop down my money, how well / if at all is flight implemented, for things like acquisition, movement, and targeting ?
     
    John-G likes this.
  40. opsive

    opsive

    Joined:
    Mar 15, 2010
    Posts:
    5,122
    41 pages and my reference for this in a different thread :) http://forum.unity3d.com/threads/behavior-designer-formations-pack.316906/#post-2349094

    Behavior Designer doesn't contain any pathfinding logic so it's more up to the underlying pathfinding implementation. The post above gives some more details, but there aren't any pathfinding assets out on the Asset Store which allow true 3D pathfinding. That post describes a few workarounds, and another workaround is that you could set a height offset on the NavMeshAgent to fake it.
     
    John-G and magique like this.
  41. Wrymnn

    Wrymnn

    Joined:
    Sep 24, 2014
    Posts:
    373
    Hello,

    I am making a decision to buy Behaviour Designer, however, I have a question whether the BD will work with my game architecture.
    I am using plain c# for game logic and data, and then using GameObject only as "renderers", they do not hold any data at all nor do not perform any logic. (e.g. entire game could be simulated without any gameobjects on scene (only one ofc)).

    My question is, would it be possible to use Behaviour Designer with this approach? Because I am looking for behaviour tree for AI, but I fear all unity ones need MonoBehaviour classes to work with.

    Also, how long does the sale last? :D
     
  42. opsive

    opsive

    Joined:
    Mar 15, 2010
    Posts:
    5,122
    The classes which actually execute the behavior tree do derive from MonoBehaviour so it would take some changes. I have heard of people attempting to strip out the UnityEngine dependencies though I don't know how successful they were

    Until next Friday :)
     
  43. LoftyTheMetroid

    LoftyTheMetroid

    Joined:
    Oct 26, 2012
    Posts:
    57
    I am also having this issue using an iMac with a Retina screen.

    I'm wondering, have you taken a look at the Supporting Retina screens section of Unity's Upgrading to Unity 5.4 page?

    Maybe that's the issue? I only just bought Behavior Designer and don't see a way for me to edit the editor code myself, so I can't personally test this.
     
  44. opsive

    opsive

    Joined:
    Mar 15, 2010
    Posts:
    5,122
    I have a proper fix for it - send me a PM with your invoice number :)
     
  45. jococo

    jococo

    Joined:
    Dec 15, 2012
    Posts:
    232
    Hello,
    Looking for a better AI solution than my own. Can the AI avoid obstacles (i.e. Tree, cliff edge, etc) and does this use navMesh or some sort of node based system or other for navigating the world? I have a very large open space forest scenario. Thanks!
     
  46. opsive

    opsive

    Joined:
    Mar 15, 2010
    Posts:
    5,122
    The base Behavior Designer package is purely a behavior tree implementation so it does not contain any pathfinding. The Movement Pack does contain tasks which can use an underlying pathfinding implementation to move your agent - this can work with the NavMesh or is integrated with A* Pathfinding Project and Apex Path if you'd rather use one of those pathfinding implementations.
     
    jococo likes this.
  47. Wrymnn

    Wrymnn

    Joined:
    Sep 24, 2014
    Posts:
    373
    Would there be some way you know about how the BD could work without needing to be attached as script to gameobject?

    Because many of my c# objects like enemies etc. exist even when their GameObjects are destroyed, since player doesnt need to see them.

    I would love to work with BD, but I am not quite sure about this system. And you are the best men to ask :D
     
  48. opsive

    opsive

    Joined:
    Mar 15, 2010
    Posts:
    5,122
    The actual behavior tree traversal happens within the BehaviorManager component and that derives from MonoBehaviour so it can receive Update callbacks and tick the tree. You can tick the tree manually so this component doesn't have to be derived from MonoBehaviour, but I haven't tried removing that inheritance to see what problems it would cause.

    The actual behavior tree initially lives within the Behavior component before the BehaviorManager component loads it for execution. This component also inherits MonoBehaviour so it can be placed as a component on the GameObject as well as receive MonoBehaviour callbacks (such as Start). If your enemies can exist when their GameObjects are destroyed, this is the component that you would need to modify to remove the MonoBehaviour dependency. After looking at the component again the only related MonoBehaviour dependencies deal with the callbacks, so I imagine that it wouldn't be too hard to change the class that it inherits from. Of course, this is also without trying to remove the dependency so there may be something that I am missing but it should be possible.

    Hopefully that helps!
     
  49. PeterB

    PeterB

    Joined:
    Nov 3, 2010
    Posts:
    366
    I have a question regarding the Utility Selector. Currently, it always selects the task with the highest utility value. I would need to extend that behaviour to include weight-based random selection, as described in this video (with Dave Mark, author of Behavioral Mathematics for Game AI):

    http://gdcvault.com/play/1015683/Embracing-the-Dark-Art-of (click on "Absolute vs. Relative")

    Can this be added to the Utility Selector? Or is possible to code custom Selectors?

    Adding this to Behavor Designer's Utility Selector would make it extremely powerful.
     
  50. opsive

    opsive

    Joined:
    Mar 15, 2010
    Posts:
    5,122
    I just added it to my todo list. If you want to get started on it ahead of time you could modify the UtilitySelector task within BehaviorDesigner/Runtime/Composites and modify the OnChildExecuted and OnReevaluationEnded method. These methods are called when either a child task has ended execution, or the Utility Selector is reevaluating all of the children to determine which child should run. In both cases you'd want to change it so instead of using the highest value, you instead use weighted random selection.
     
    PeterB likes this.