How to Code Behaviour Trees in Unity C#

Поделиться
HTML-код
  • Опубликовано: 20 сен 2024

Комментарии • 109

  • @git-amend
    @git-amend  4 месяца назад +5

    Hi everyone! Happy Sunday! Make sure to hit the 👍 and the 🔔 if you don't want to miss next week's video that will continue this topic into Blackboard Architecture! Hope you like it!
    Also, check out the little bash script I included in the repo. If you are like me, and you use CTRL-ALT-A to stage your files in your IDE, it doesn't include the meta files, which you might want to include. The `stageMeta` script will find all the missing meta files and stage them for you.

  • @codecombustion7878
    @codecombustion7878 4 месяца назад +14

    Dude.. I don't know how you don't have more subscribers.
    I used to do basic gamedev back in the late '90's/00's and have tried to get back into it in the last couple years (off and on) without writing my own openGL based engine again.
    It's refreshing to find tutorials that not aimed at absolute beginners. I've been programming professionally for 20 years -- I just want help with structure/systems/engine specifics. This is great.

    • @git-amend
      @git-amend  4 месяца назад

      Thanks for the kind words, I appreciate the comment!

  • @oldshamen
    @oldshamen 4 месяца назад +7

    This channel is a pure gem. I am really impressed that you manage to put out these quality videos on a weekly basis.

    • @git-amend
      @git-amend  4 месяца назад

      Wow, thank you! Thanks for the kind words!

  • @Hersatz
    @Hersatz 4 месяца назад +5

    Great stuff, as always!
    For those interested, there are ways to migrate the logic into a UI grid with nodes and links through UI Toolkit. You can even throw a mini-map in the lot. It's quite nice.
    The final product is far more scalable and readable than code only. It also allows compartmentalization of blocs of logic to reuse them in other trees and more! Level up your complex systems with visuals that boosts productivity 😀.

    • @git-amend
      @git-amend  4 месяца назад

      Thanks for sharing that tip, it's a great idea!

    • @rize2137
      @rize2137 4 месяца назад +1

      Would love to see that

    • @TheKr0ckeR
      @TheKr0ckeR 4 месяца назад

      there is a dedicated tutorial for that using UI Builder and Graph View.. ruclips.net/video/nKpM98I7PeM/видео.html
      Mixing those with your tutorial will be the ultimate behaviour tree to be use.

    • @rize2137
      @rize2137 4 месяца назад +1

      @@TheKr0ckeR gonna check that out, thanks

  • @jumpjumpdiegaming
    @jumpjumpdiegaming 2 месяца назад +1

    This is just another banger to be frank. You broke it down so simply I was able to grasp the concepts quickly and create my own behaviour tree with different functionality! Legend.

    • @git-amend
      @git-amend  2 месяца назад

      Glad to hear that! Thank you!

  • @neatodev2249
    @neatodev2249 4 месяца назад +4

    Your Unity-C# tutorials are among the best on RUclips and you deserve much higher viewership. It's so hard to find scripting tutorials any more advanced than a basic introduction to design patterns, or aren't overly niche... And yet you write truly practical, useful, and properly structured code with a high production value, and you pump these things out like a beast! We must protect this man at all costs!
    Quick question although this may have been answered in a previous video: where did you get that editor script for drag-ang-drop arrays? Did you make it yourself or is it an asset?

    • @git-amend
      @git-amend  4 месяца назад +1

      Thanks for the comment. I think the functionality you are referring to is just built in with Odin Inspector, but I'd have to double check.

  • @goma8167
    @goma8167 2 месяца назад

    I am very glad someone mentionned this RUclips channel, somewhere, I don't remember where. Nice topics you've got there, thanks for sharing. I can't wait to watch more of your videos.

    • @git-amend
      @git-amend  2 месяца назад +1

      Welcome aboard!

  • @Sparklmonkey
    @Sparklmonkey 4 месяца назад

    I haven't watched the full video yet, but I have spent the last week looking for the best implementation for a simple AI for my card game. It is almost like a sign from the Universe. You always have the perfect video. Can't wait to learn and implement this

    • @git-amend
      @git-amend  4 месяца назад +1

      Great to hear! Hope it helps!

  • @AliHosseiniDev
    @AliHosseiniDev 3 месяца назад

    Nice video! it just popped on my home page exactly when i was going to code my AI and now i will implement it with this pattern!
    im pretty sure your channel will grow even more than brackeys.

    • @git-amend
      @git-amend  3 месяца назад +1

      Glad it was helpful! Thanks for the comment!

  • @BendNaTe
    @BendNaTe 4 месяца назад

    I've watched all of your videos but this is my first time commenting! I really love your style and approach. This channel has become my favorite channel in the game dev space on RUclips. I've used Unity for around 5 years and my current live service game is a Unity game. However, the past year or so I have transitioned to Godot for a variety of reasons. Using your approaches and methodologies to help me with better architecting systems in a new engine such as Godot has been a blast! All of the knowledge you expel is so easily transferable. I really appreciate all you do and I hope you continue making videos like these for the foreseeable future.

    • @git-amend
      @git-amend  4 месяца назад

      Thanks for the great message! I'm glad you are finding it easy to transfer topics from the channel to Godot! Nice when that works out! Lots more to come!

  • @grahammccarthy
    @grahammccarthy 4 месяца назад

    You consistently have the best tutorials out there right now. You'll be at 100k subscribes in no time! keep up the great work.

    • @git-amend
      @git-amend  4 месяца назад +1

      Hope so! Thanks for the encouraging feedback!

  • @TheArghnono
    @TheArghnono 4 месяца назад

    Excellent video! I am using the Fluid Behavior Tree in my current main project. A well.implemented BT makes it so easy to keep track of and debug agent AIs.

    • @git-amend
      @git-amend  4 месяца назад

      Awesome! I agree, makes life easier!

  • @drewalkemade3715
    @drewalkemade3715 4 месяца назад +1

    Awesome implementation! Looking forward to the blackboard.

    • @git-amend
      @git-amend  4 месяца назад

      Coming soon!

    • @puntalic
      @puntalic 4 месяца назад

      Yeah me too.

  •  4 месяца назад

    You are doing the best tutorials in known Internet....Keep doing!

    • @git-amend
      @git-amend  4 месяца назад

      Glad you like them!

  • @weeqn1967
    @weeqn1967 4 месяца назад

    Wow, excellent video! I just finished udemy course about Behaviour Trees and it's seems that your approach is waaay more flexible and understandable. I'm really looking forward to see the next video about blackboard (:

    • @git-amend
      @git-amend  4 месяца назад

      Awesome! See you next Sunday then!

  • @정동우-n2x
    @정동우-n2x 4 месяца назад +1

    I love It. it was what I needed the most.
    Thank you very much.

    • @git-amend
      @git-amend  4 месяца назад +1

      Great! I'm glad to hear that! I'm sure you will like next week's video too.

    • @정동우-n2x
      @정동우-n2x 4 месяца назад

      @@git-amend There's never been a time when I didn't like your video.
      However, this video is "LOVE".!!

  • @Dev1gne
    @Dev1gne 4 месяца назад

    Another awesome AI video after GOAP, already looking forward to coding along and learning something new!

    • @git-amend
      @git-amend  4 месяца назад

      Great! Hope you like it!

  • @AndrewYakymetsLS
    @AndrewYakymetsLS 4 месяца назад +1

    Thanks! This is exactly what I was looking for.

    • @git-amend
      @git-amend  4 месяца назад

      Glad to hear it!

  • @Santiago-bl3xu
    @Santiago-bl3xu 4 месяца назад

    An interesting extension for performance could be integrating the BT and/or some nodes with an event-system to handle interruptions & environment changes rather than relying entirely on the entity's class Update method. Alternatively, you could go down the coroutine/task path and use a fixed time-interval to reduce the number of environment/condition evaluations (e.g. every 1/10th of a second rather than at every frame). If you have 100s of NPCs/AIs, or are targeting mobile, this could make a difference.

    • @git-amend
      @git-amend  4 месяца назад +2

      Yes, there is definitely room for some performance enhancements. Rate limiting is probably chief among them. We'll talk a bit more about handling environment changes and additional data in next week's video! Thanks for the comment!

  • @isleeg
    @isleeg 4 месяца назад +1

    Little tip! If your waypoints are too close to each other then agent.pathPending will never be true and your hero will not go to the next waypoint. I think because the calculation will be that quick that the path is ready before we could check it.

    • @git-amend
      @git-amend  4 месяца назад

      Great tip! Thanks for pointing that out!

  • @vic137
    @vic137 2 месяца назад

    Woww.
    And here I am spending a month on if statements and switch cases to make my AI

    • @git-amend
      @git-amend  2 месяца назад

      Nice, big improvement then.

  • @Kzyh43
    @Kzyh43 4 месяца назад

    Thanks! Awesome as usual!

  • @BrandonLing-ko6hb
    @BrandonLing-ko6hb 4 месяца назад

    amazing video yet again!

  • @MSLICENT
    @MSLICENT 2 месяца назад +1

    I notice that, in selector node, process will start from current child, but in priority selector, process starts from the first child. So we must use priority selector to do some interrupt?

  • @damonfedorick
    @damonfedorick 4 месяца назад +1

    Nice

  • @P1casse
    @P1casse 2 месяца назад

    Great video as always

    • @git-amend
      @git-amend  2 месяца назад +1

      There are a few NodeCanvas assets on the store. I only have one of them, the one made by Paradox Notion, and I think it is excellent. My advice is definitely to either use a visual tool like this or make your own, once you have a good understanding of Behaviour Trees. That way you can either adapt your solution to that tool, or go the other way and extend that tool to suit your needs. On a larger BT I would definitely advise some kind of visual aid.

  • @xbbotz866
    @xbbotz866 4 месяца назад

    god-like tutorial

  • @OskGame
    @OskGame 4 месяца назад

    So great, keep it up!!!

    • @git-amend
      @git-amend  4 месяца назад

      Thank you! Will do!

    • @OskGame
      @OskGame 4 месяца назад

      @@git-amend Your knowledge is very good for people who are developing advanced skills, to be honest, I need to gradually digest them, DI is something I want to apply first for small projects that scale and maintain into large projects

  • @abraiyan7984
    @abraiyan7984 4 месяца назад

    Thanks for this 🙏

    • @git-amend
      @git-amend  4 месяца назад

      My pleasure 😊

  • @neetshonen
    @neetshonen 3 месяца назад

    I have noticed that you do the execution of the behaviour tree on the "Update" function.
    The thing is , if you do a complex strategy , lets say for an fps game where you have to move/check for enemies/check for whatever/do the action.
    and lets say that you have 50 npcs with that strategy , then your complexity is super huge for the proccessor .
    What do you recommend in that case ?

    • @git-amend
      @git-amend  3 месяца назад

      In this case I would suggest implementing an execution policy. If you take a look at the repository, I've actually included a few policies in the root node. There you could have a timer so you only run once every few milliseconds instead of every single frame - this way you can throttle the processing - in most cases this is overkill. You could also throttle outside of the BT, but that's up to you!

  • @lucianobisconti9078
    @lucianobisconti9078 4 месяца назад

    Hello, I'm learning a lot from your videos, your explanations are great and the code sometimes seems complex but it's great. Could you make videos based on different project architectures?

    • @lucianobisconti9078
      @lucianobisconti9078 4 месяца назад

      also advantages and disadvantages of each project architecture, and what things should be taken into account when choosing a certain architecture

    • @git-amend
      @git-amend  4 месяца назад

      Slowly but surely we'll cover all sorts of game architecture topics like this!

  • @mertakbulut241
    @mertakbulut241 4 месяца назад

    Thanks for the video sir.
    Can you do some video about "Custom Package Publishing" ?

    • @git-amend
      @git-amend  4 месяца назад +1

      You are welcome. Yes, I have a video about creating packages coming soon!

  • @ninqbi1738
    @ninqbi1738 3 месяца назад

    This vid is so useful. Btw, do u know how to make the shader effect for your character when it's concealed by the wall or something else. Thank you!

    • @git-amend
      @git-amend  3 месяца назад +1

      Thank you. Actually there is a great video on this topic that was made by Nik Lever on the Unity channel about this. He talks about 3 ways to use URP render features, and I believe it's the 3rd technique. ruclips.net/video/3CpEn_mmr3o/видео.html

  • @TheKr0ckeR
    @TheKr0ckeR 4 месяца назад

    Great guide as always! Where do you seperate logic which is not common? For example, Your character is moving to patrol points with "animation" even if you didnt call it inside Patrol, so do you just call it in another place where you check magnitude etc. I want to make the strategies modular as possible, but there are some typical conditions like animation running, or tweening while moving etc. Should i seperate the logic there, or create another interface to handle that. As far as i see, you seperated the animation logic from the strategies. Or instead of passing, agent, target, speed etc. should i directly pass "Zombie" or smth to the strategy constructors for better usage? I generally find myself setting the logic inside Zombie (live move methods etc.), and in strategies, I am just calling them. I dont know its a good way though. I cant seem to understand how to seperate them, for example there should be a Die Strategy where we implement die methods to call ragdoll etc, or should i simply call zombie.Die(); inside the strategy and actions/events would do the rest like ragdolls etc. So to summarize, writing the logic inside Strategy, or calling it from given class

    • @git-amend
      @git-amend  4 месяца назад +1

      Well, this is a question that could have multiple answers, and there is no cookie-cutter solution. Here are a few guidelines (by no means rules). Strategies should primarily encapsulate decision-making logic. Keep strategies focused on decision-making, delegating execution through interfaces or events - so if a strategy needs to execute an animation, either pass in an Action that will trigger the animation change, or fire and event. Another option is to completely separate animations from the behaviour tree and whatever node is currently being evaluated. In my example, the behaviour tree does not handle animation at all - the Hero updates the Speed float of the AnimationController based on the velocity of the NavMeshAgent.
      Regardless, I think you are going to see an interesting solution on Sunday when we look at Blackboard architecture.

    • @TheKr0ckeR
      @TheKr0ckeR 4 месяца назад

      @@git-amend Cant wait to see blackboard and data sharing! I was using State machine for my zombies and showed your BT and implemented. Had hard times sharing data between nodes like checking targret and attacking them. Just waiting your approach to make it better. Thanks for your gold answer and time.

  • @ragerungames
    @ragerungames 4 месяца назад

    Awesome video... ❤

    • @git-amend
      @git-amend  4 месяца назад

      Glad you liked it

  • @MarushiaDark316
    @MarushiaDark316 4 месяца назад

    When adding nodes, can you just chain them together, like you do for a Builder?
    So like node.AddChild(child1).AddChild(child2).AddChild(child3);

    • @git-amend
      @git-amend  4 месяца назад +1

      Yes, you can apply a Fluent Builder pattern to this very easily.

  • @ninqbi1738
    @ninqbi1738 2 месяца назад

    Can i let All the nodes action or condition inherit from Leaf directly instead of using IStrategy (of course i will remove this interface, only use Leaf class)?. And how to integrate blackboard to behavior tree?. That's all my wondering, im so gratefull if you have time to answer me. Thank you!!!

  • @olegtopalov4120
    @olegtopalov4120 4 месяца назад

    Amazing video! One question - how does Behaviour tree compare to StateMachines? Could them be combined, or it's just too excessive?

    • @git-amend
      @git-amend  4 месяца назад +1

      Great question! A BT is very similar to a Hierarchical State Machine. There are pros and cons to both.
      A state machine offers clear delineation of states and transitions, simplifying the logic flow. However, they can become cumbersome when handling numerous states or complex transitions, potentially leading to bloated code and reduced maintainability compared to more dynamic approaches like BTs.
      And yes, the can be combined - that is going to be the subject of a video in a few weeks!

  • @Wanderlust_Duo
    @Wanderlust_Duo 4 месяца назад

    Thank you very much for the tutorial! I have a little problem. After the final code, the execution logic doesn't continue after the sequence is executed. For example, if the hero got the treasure, then nothing further happens. If he became in a safe position, then after that, he doesn't react to any changes, etc. The code has already checked 10 times, everything is identical to yours (I guess).

    • @git-amend
      @git-amend  4 месяца назад +1

      Check the repository link in the description. I added the concept of Policies for how long to execute to the main Behaviour Tree node (after recording)

    • @Wanderlust_Duo
      @Wanderlust_Duo 4 месяца назад

      @@git-amend ok, thanks, I'll check it:)

    • @joeb3017
      @joeb3017 Месяц назад

      I know I'm a few months late, but the issue seems to be that in the Behavior Tree class, the Process() method should have "currentChild = (currentChild + 1) % children.Count;" before "return Status.Success" (second to last line of the method). If it doesn't do this, then the currentChild will never be set back to 0 and thus the whole tree will only ever run once. He actually briefly references this at 7:48. This only works if you always want your behavior trees running forever and you don't plan on making subtrees.
      I'm guessing he did it this way so that you can have functioning subtrees?

  • @laserbean00001
    @laserbean00001 4 месяца назад

    Do you have a video on menu systems yet?

    • @git-amend
      @git-amend  4 месяца назад +1

      Last year I made one for UGUI that might interest you: ruclips.net/video/Wir7jMp5v_k/видео.html

  • @Kzyh43
    @Kzyh43 4 месяца назад

    Hey. I was wondering if UntilFail Node should return Success when the child returns Failure?

    • @git-amend
      @git-amend  4 месяца назад +2

      For me, that would seem counter intuitive because I'm considering the total outcome. But if that makes more sense to you, then by all means you can use it that way, I can understand how it could be interpreted at succeeding at failure.

  • @omeryilmaz1021
    @omeryilmaz1021 4 месяца назад

    Whats the benefits of implementing such a state management with strategy pattern instead of using standart abstraction of state machine

    • @git-amend
      @git-amend  4 месяца назад

      Using the strategy pattern provides flexibility by allowing dynamic node behaviors at runtime, enhancing adaptability to changing requirements. This approach allows for easier maintenance and extensibility compared to a standard state machine. You could just as easily use the strategy pattern in a state machine as well.

  • @fleity
    @fleity 4 месяца назад +2

    Probably has been asked a 1000 times before but anyway, which tool do you use to draw your diagrams?

    • @git-amend
      @git-amend  4 месяца назад +1

      Haha yeah, it gets asked a lot. I should put it in the description of all the videos maybe. Anyway, I use Excalidraw for Obsidian for all my diagrams these days. You can see more about that here: ruclips.net/video/o0exK-xFP3k/видео.html

  • @-moristan109
    @-moristan109 4 месяца назад

    Great video as usual, I have a question about the BT and the Goab, how could we integrate the Goab system to pick and create nodes and sequences to archive a goal, so the nodes would be an actions but the action wont be a one class but a combination of nodes, is that has any logic or it's just two deferent soultions for two different problems on the same Ai agent.

    • @git-amend
      @git-amend  4 месяца назад

      Interesting question for sure. In most cases you would not need to mix and match the two. But if you did want to, it could involve using GOAP's planning capabilities to dynamically generate sequences of behavior tree nodes to achieve goals. This would potentially offer a flexible and adaptive approach to decision-making. This integration allows for leveraging the strengths of both systems: GOAP's goal-oriented planning and behavior trees' hierarchical structure for action execution.
      I think it would be fairly complex to implement and debug, but definitely an interesting idea!

  • @falgunpawar
    @falgunpawar 6 дней назад

    Hey , In my hero class I don't have input and animation so I had not used enable and disable.. the rest of the code is same. But when I toggle is in danger it goes to the resting spot, but then when I toggle it back, it stays there. Also after picking up both the treasures, it keeps standing at the last treasure place and does not go into patrolling.. Am I missing something? Could you please help?. the rest of the code is same as even tested by downloading the git code.. Thank you in advance!

    • @git-amend
      @git-amend  6 дней назад

      It's tough to solve problems like that without more information. I suggest you join the Discord and ask in the video-forum-discussions, and someone will likely be able to give you some advice.

    • @falgunpawar
      @falgunpawar 6 дней назад

      @@git-amend okay, thank you so much!

  • @paulushcgcj
    @paulushcgcj 4 месяца назад

    Loving your videos. If it's not too much to ask, would you keep your code structured in a way that the same code can be easily replicated in other tools or engines too? As a seasoned dev I can port your code to my monogame or even godot, but for those who are just learning using other engines, it could be harder.

  • @italoamaralnvcc
    @italoamaralnvcc 4 месяца назад

    Amazing stuff!
    Quick question.... What do you use to make graphs like at 1:20? It seems very useful.

    • @git-amend
      @git-amend  4 месяца назад +1

      Thanks! I use Excalidraw for Obsidian - can see that here: ruclips.net/video/o0exK-xFP3k/видео.html

  • @gwm249
    @gwm249 3 месяца назад

    What is the whiteboard app been used here in the intro?

    • @git-amend
      @git-amend  3 месяца назад

      That's Excalidraw for Obsidian, though you can use it standalone too.

  • @crazyfox55
    @crazyfox55 4 месяца назад

    But what if I just put this in a big if statement?

  • @SuperDude101
    @SuperDude101 3 месяца назад

    Noice 👍

  • @ranejeb
    @ranejeb 4 месяца назад

    At first glance BahviorTree node and Sequence node look both the same...

    • @git-amend
      @git-amend  4 месяца назад

      Yes very similar. One subtle difference if you take a look in the repository you’ll see I added Policies to the BT Node, but it didn’t make it into the video.