How to Code Behaviour Trees in Unity C#

  Рет қаралды 8,538

git-amend

git-amend

Күн бұрын

Understanding Behavior Trees lays the foundation for understanding the more advanced topics such as Blackboard Architecture. Behavior Trees are a hierarchical decision-making model commonly used in AI and game development. Walk through creating all the most important nodes in a clean, easy to understand implementation.
Want to support me? 😀
Buy me a coffee! ko-fi.com/adammyhre
🔔 Subscribe for more Unity Tutorials / @git-amend
Discord: / discord
#unity3d #gamedev #indiedev
▬ Contents of this video ▬▬▬▬▬▬▬▬▬▬
0:00 Behaviour Trees Overview
2:16 Nodes
4:50 Behaviour Tree
8:00 Sequence Node
11:20 Selector Node
13:55 Priority Selector
19:30 Complex Behaviour
Source code: github.com/adammyhre/Unity-Be...
Assets Shown In This Video (Affiliate Links)
Dungeon Mason Tiny Hero Duo: (FREE): assetstore.unity.com/packages...
Tiny Heroes: assetstore.unity.com/packages...
Tidal Flask Dungeon Pack: assetstore.unity.com/packages...
Hierarchy Icons: assetstore.unity.com/packages...
Better Transform: assetstore.unity.com/packages...
Better Mesh Filter: assetstore.unity.com/packages...
Kyeoms VFX: assetstore.unity.com/publishe...
Follow me!
linktr.ee/gitamend

Пікірлер: 98
@git-amend
@git-amend 2 ай бұрын
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.
@jumpjumpdiegaming
@jumpjumpdiegaming 2 күн бұрын
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!
@codecombustion7878
@codecombustion7878 2 ай бұрын
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 2 ай бұрын
Thanks for the kind words, I appreciate the comment!
@oldshamen
@oldshamen 2 ай бұрын
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 2 ай бұрын
Wow, thank you! Thanks for the kind words!
@neatodev2249
@neatodev2249 2 ай бұрын
Your Unity-C# tutorials are among the best on KZfaq 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 2 ай бұрын
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.
@Hersatz
@Hersatz 2 ай бұрын
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 2 ай бұрын
Thanks for sharing that tip, it's a great idea!
@rize2137
@rize2137 Ай бұрын
Would love to see that
@TheKr0ckeR
@TheKr0ckeR Ай бұрын
there is a dedicated tutorial for that using UI Builder and Graph View.. kzfaq.info/get/bejne/pLGggJxprp20lYE.html Mixing those with your tutorial will be the ultimate behaviour tree to be use.
@rize2137
@rize2137 Ай бұрын
@@TheKr0ckeR gonna check that out, thanks
@AliHosseiniDev
@AliHosseiniDev Ай бұрын
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 Ай бұрын
Glad it was helpful! Thanks for the comment!
@AndrewYakymetsLS
@AndrewYakymetsLS 2 ай бұрын
Thanks! This is exactly what I was looking for.
@git-amend
@git-amend 2 ай бұрын
Glad to hear it!
@Sparklmonkey
@Sparklmonkey 2 ай бұрын
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 2 ай бұрын
Great to hear! Hope it helps!
@BendNaTe
@BendNaTe Ай бұрын
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 KZfaq. 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 Ай бұрын
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!
@user-rm2pj9jf8s
@user-rm2pj9jf8s 2 ай бұрын
I love It. it was what I needed the most. Thank you very much.
@git-amend
@git-amend 2 ай бұрын
Great! I'm glad to hear that! I'm sure you will like next week's video too.
@user-rm2pj9jf8s
@user-rm2pj9jf8s 2 ай бұрын
@@git-amend There's never been a time when I didn't like your video. However, this video is "LOVE".!!
@TheArghnono
@TheArghnono 2 ай бұрын
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 2 ай бұрын
Awesome! I agree, makes life easier!
@grahammccarthy
@grahammccarthy 2 ай бұрын
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 2 ай бұрын
Hope so! Thanks for the encouraging feedback!
Ай бұрын
You are doing the best tutorials in known Internet....Keep doing!
@git-amend
@git-amend Ай бұрын
Glad you like them!
@drewalkemade3715
@drewalkemade3715 2 ай бұрын
Awesome implementation! Looking forward to the blackboard.
@git-amend
@git-amend 2 ай бұрын
Coming soon!
@puntalic
@puntalic 2 ай бұрын
Yeah me too.
@Kzyh43
@Kzyh43 2 ай бұрын
Thanks! Awesome as usual!
@git-amend
@git-amend 2 ай бұрын
Thanks again!
@Dev1gne
@Dev1gne 2 ай бұрын
Another awesome AI video after GOAP, already looking forward to coding along and learning something new!
@git-amend
@git-amend 2 ай бұрын
Great! Hope you like it!
@BrandonLing-ko6hb
@BrandonLing-ko6hb 2 ай бұрын
amazing video yet again!
@git-amend
@git-amend 2 ай бұрын
Thank you!
@OskGame
@OskGame Ай бұрын
So great, keep it up!!!
@git-amend
@git-amend Ай бұрын
Thank you! Will do!
@OskGame
@OskGame Ай бұрын
@@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
@weeqn1967
@weeqn1967 Ай бұрын
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 Ай бұрын
Awesome! See you next Sunday then!
@abraiyan7984
@abraiyan7984 2 ай бұрын
Thanks for this 🙏
@git-amend
@git-amend 2 ай бұрын
My pleasure 😊
@Santiago-bl3xu
@Santiago-bl3xu 2 ай бұрын
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 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!
@ragerungames
@ragerungames 2 ай бұрын
Awesome video... ❤
@git-amend
@git-amend 2 ай бұрын
Glad you liked it
@xbbotz866
@xbbotz866 Ай бұрын
god-like tutorial
@git-amend
@git-amend Ай бұрын
Thanks!
@damonfedorick
@damonfedorick 2 ай бұрын
Nice
@git-amend
@git-amend 2 ай бұрын
Thanks
@isleeg
@isleeg 2 ай бұрын
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 2 ай бұрын
Great tip! Thanks for pointing that out!
@lucianobisconti9078
@lucianobisconti9078 2 ай бұрын
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 2 ай бұрын
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 2 ай бұрын
Slowly but surely we'll cover all sorts of game architecture topics like this!
@MSLICENT
@MSLICENT 8 күн бұрын
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?
@neetshonen
@neetshonen Ай бұрын
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 Ай бұрын
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!
@-moristan109
@-moristan109 2 ай бұрын
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 2 ай бұрын
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!
@mertakbulut241
@mertakbulut241 Ай бұрын
Thanks for the video sir. Can you do some video about "Custom Package Publishing" ?
@git-amend
@git-amend Ай бұрын
You are welcome. Yes, I have a video about creating packages coming soon!
@laserbean00001
@laserbean00001 Ай бұрын
Do you have a video on menu systems yet?
@git-amend
@git-amend Ай бұрын
Last year I made one for UGUI that might interest you: kzfaq.info/get/bejne/jc-ias1-1Zvaj58.html
@ninqbi1738
@ninqbi1738 19 күн бұрын
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 19 күн бұрын
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. kzfaq.info/get/bejne/aamgeNGQ0tPWY6M.html
@fleity
@fleity 2 ай бұрын
Probably has been asked a 1000 times before but anyway, which tool do you use to draw your diagrams?
@git-amend
@git-amend 2 ай бұрын
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: kzfaq.info/get/bejne/pZaVq65e3ay0Y58.html
@gwm249
@gwm249 17 күн бұрын
What is the whiteboard app been used here in the intro?
@git-amend
@git-amend 17 күн бұрын
That's Excalidraw for Obsidian, though you can use it standalone too.
@TheKr0ckeR
@TheKr0ckeR Ай бұрын
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 Ай бұрын
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 Ай бұрын
@@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.
@SuperDude101
@SuperDude101 28 күн бұрын
Noice 👍
@git-amend
@git-amend 27 күн бұрын
Thank you!
@olegtopalov4120
@olegtopalov4120 2 ай бұрын
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 2 ай бұрын
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!
@Kzyh43
@Kzyh43 Ай бұрын
Hey. I was wondering if UntilFail Node should return Success when the child returns Failure?
@git-amend
@git-amend Ай бұрын
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.
@MarushiaDark316
@MarushiaDark316 Ай бұрын
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 Ай бұрын
Yes, you can apply a Fluent Builder pattern to this very easily.
@Wanderlust_Duo
@Wanderlust_Duo Ай бұрын
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 Ай бұрын
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 Ай бұрын
@@git-amend ok, thanks, I'll check it:)
@ninqbi1738
@ninqbi1738 5 күн бұрын
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!!!
@omeryilmaz1021
@omeryilmaz1021 Ай бұрын
Whats the benefits of implementing such a state management with strategy pattern instead of using standart abstraction of state machine
@git-amend
@git-amend Ай бұрын
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.
@crazyfox55
@crazyfox55 Ай бұрын
But what if I just put this in a big if statement?
@italoamaralnvcc
@italoamaralnvcc Ай бұрын
Amazing stuff! Quick question.... What do you use to make graphs like at 1:20? It seems very useful.
@git-amend
@git-amend Ай бұрын
Thanks! I use Excalidraw for Obsidian - can see that here: kzfaq.info/get/bejne/pZaVq65e3ay0Y58.html
@paulushcgcj
@paulushcgcj Ай бұрын
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.
@ranejeb
@ranejeb 2 ай бұрын
At first glance BahviorTree node and Sequence node look both the same...
@git-amend
@git-amend 2 ай бұрын
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.
Mediator Pattern - Reduce Chaotic Dependencies
15:47
git-amend
Рет қаралды 6 М.
SIMPLE Tip For Better Unity Game Architecture
12:57
git-amend
Рет қаралды 27 М.
Khóa ly biệt
01:00
Đào Nguyễn Ánh - Hữu Hưng
Рет қаралды 22 МЛН
Must-have gadget for every toilet! 🤩 #gadget
00:27
GiGaZoom
Рет қаралды 12 МЛН
NERF WAR HEAVY: Drone Battle!
00:30
MacDannyGun
Рет қаралды 41 МЛН
Пробую самое сладкое вещество во Вселенной
00:41
Better Singletons in Unity C#
14:08
git-amend
Рет қаралды 7 М.
How to Implement Blackboard Architecture in Unity C#
28:57
git-amend
Рет қаралды 9 М.
IEnableableComponent ile State işlemleri
17:43
Berk Terek
Рет қаралды 183
Learn to Build an Advanced Event Bus | Unity Architecture
13:27
Additive Async Multi-Scene Loading in Unity
16:59
git-amend
Рет қаралды 10 М.
D in SOLID - I wish I learned the LAST letter FIRST
18:00
git-amend
Рет қаралды 7 М.
The Unity HACK that the PROS know
21:27
git-amend
Рет қаралды 10 М.
A new way to generate worlds (stitched WFC)
10:51
Watt Designs
Рет қаралды 513 М.