Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Any Behavior Tree Guides and examples?

Discussion in 'Scripting' started by ILemonDev, Mar 29, 2016.

  1. ILemonDev

    ILemonDev

    Joined:
    Dec 23, 2015
    Posts:
    11
    Hey guys

    So i am looking at building my own AI for my game. I have usually built finite state machines to run my AI from beginning with a simple switch statement to having difference classes represent states. Now i would like to learn about implementing a behavior tree for my AI. I have looked at the article of gamasutra and it's really good in helping me understand the core concept of what a behavior tree is and what each part does but i don't know how i would go about coding the different parts of a tree.

    My knowledge of implementing it goes as far as understanding that the actual leaf/leaves of a tree are individual classes which represent that behavior. But I don't know how a selector would run and call the next child of the tree if the first one failed.

    So are there any decent implementation guides that i can have a look at? Or does anyone have any idea on how a node on a behavior tree would be called or how are the base leaves implemented. Thanks :)
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    eses and ericbegue like this.
  3. ericbegue

    ericbegue

    Joined:
    May 31, 2013
    Posts:
    1,353
    Thank you @BoredMormon for suggesting Panda Behaviour!

    @Pristo
    It's great that you've already read the gamasutra article. If you want some more reading, I'd suggest these articles. Since you already know the core concepts about Behaviour Tree, you can directly dive into writing the behaviour for your AI by using Panda BT.

    In Panda BT, the leave nodes (the tasks) are implemented as simple C# methods (less tedious than classes to implement).

    For example, this would be a task to move the GameObject to some destination:
    Code (CSharp):
    1. using UnityEngine;
    2. using Panda;
    3.  
    4. class UnitAI : MonoBehaviour
    5. {
    6.     float speed;
    7.     Vector3 destination;
    8.  
    9.     [Task]
    10.     void MoveToDestination()
    11.     {
    12.         // Where are we going?
    13.         var delta = (destination - this.transform.position);
    14.         var velocity = delta.normalized * speed;
    15.  
    16.         // Update position
    17.         this.transform.position = this.transform.position + velocity * Time.deltaTime;
    18.  
    19.         // Are we there yet?
    20.         if ( Vector3.Distance( destination, this.transform.position ) < 0.01f )
    21.         {
    22.             Task.current.Succeed();
    23.         }
    24.     }
    25.  
    26.     // You can implement more tasks here.
    27.  
    28. }
    Then these tasks are used as building blocks to write BT scripts. A BT script is a textual representation of your Behaviour Tree. The BT scripts are executed through the PandaBehaviour component, which you attach on a GameObject you want to drive using a BT. Also, you can visualize the execution of the tree directly in the Inspector through this component.

    This is a screenshot of the PandaBehaviour component running a BT Script:



    I'd be glad to help if you have any question.

    This is the Panda BT website:

    http://www.pandabehaviour.com/

    Also, you're welcome on this thread:

    http://forum.unity3d.com/threads/ai-scripting-panda-bt-behaviour-tree-scripting.388429/
     
    Last edited: Mar 30, 2016
    Deleted User and ILemonDev like this.
  4. ILemonDev

    ILemonDev

    Joined:
    Dec 23, 2015
    Posts:
    11
    Thankyou guys for referencing panda. I will have a look at that tonight. Also those guides are amazing thanks
     
    Last edited: Jun 7, 2020
  5. ericbegue

    ericbegue

    Joined:
    May 31, 2013
    Posts:
    1,353
    These references was actually great resources for me while I designed Panda BT.

    Great that you are giving it a try! Hope you'll like it.
     
    Deleted User likes this.