How to Code Behaviour Trees in Unity C#

  Рет қаралды 17,928

git-amend

git-amend

Күн бұрын

Пікірлер
@git-amend
@git-amend 6 ай бұрын
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 6 ай бұрын
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 6 ай бұрын
Thanks for the kind words, I appreciate the comment!
@AITools-s8f
@AITools-s8f 2 ай бұрын
I found a good way to organize the tree: Use Unity's Hierarchy tree. Basically, make node a gameObject and add it as children of the tree. Then you can process nodes in sequence of child index. The good thing is that you can drag around any node without changing scripts. you can read the status of each node by changing the name of respected gameObject
@git-amend
@git-amend 2 ай бұрын
That's a very interesting approach. I'll have to try that one out!
@147-m6l
@147-m6l 14 күн бұрын
This channel is the only one I found that has the correct implementation of the sequence compositor. Every other tutorial implements a parallel or parallel sequence and calls it a sequence which is really misleading. Thanks a lot mate!
@git-amend
@git-amend 14 күн бұрын
Great to hear!
@oldshamen
@oldshamen 6 ай бұрын
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 6 ай бұрын
Wow, thank you! Thanks for the kind words!
@goma8167
@goma8167 4 ай бұрын
I am very glad someone mentionned this KZbin 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 4 ай бұрын
Welcome aboard!
@lukasbannink7944
@lukasbannink7944 3 күн бұрын
Man, idk if you're just starting out or something, but I don't get how your channel isn't bigger. These videos are absolutely amazing. Keep up the good work man. This is a very rare occasion where I actually like and subscribe before even finishing the video
@git-amend
@git-amend 3 күн бұрын
Kind words! Glad to have you here!
@Hersatz
@Hersatz 6 ай бұрын
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 6 ай бұрын
Thanks for sharing that tip, it's a great idea!
@rize2137
@rize2137 6 ай бұрын
Would love to see that
@TheKr0ckeR
@TheKr0ckeR 6 ай бұрын
there is a dedicated tutorial for that using UI Builder and Graph View.. kzbin.info/www/bejne/pHzTfmxufpyDm68 Mixing those with your tutorial will be the ultimate behaviour tree to be use.
@rize2137
@rize2137 6 ай бұрын
@@TheKr0ckeR gonna check that out, thanks
@neatodev2249
@neatodev2249 6 ай бұрын
Your Unity-C# tutorials are among the best on KZbin 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 6 ай бұрын
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.
@jumpjumpdiegaming
@jumpjumpdiegaming 4 ай бұрын
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 4 ай бұрын
Glad to hear that! Thank you!
@isleeg
@isleeg 6 ай бұрын
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 6 ай бұрын
Great tip! Thanks for pointing that out!
@Sparklmonkey
@Sparklmonkey 6 ай бұрын
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 6 ай бұрын
Great to hear! Hope it helps!
@BendNaTe
@BendNaTe 6 ай бұрын
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 KZbin. 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 6 ай бұрын
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!
@AliHosseiniDev
@AliHosseiniDev 5 ай бұрын
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 5 ай бұрын
Glad it was helpful! Thanks for the comment!
@grahammccarthy
@grahammccarthy 6 ай бұрын
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 6 ай бұрын
Hope so! Thanks for the encouraging feedback!
@drewalkemade3715
@drewalkemade3715 6 ай бұрын
Awesome implementation! Looking forward to the blackboard.
@git-amend
@git-amend 6 ай бұрын
Coming soon!
@puntalic
@puntalic 6 ай бұрын
Yeah me too.
6 ай бұрын
You are doing the best tutorials in known Internet....Keep doing!
@git-amend
@git-amend 6 ай бұрын
Glad you like them!
@TheArghnono
@TheArghnono 6 ай бұрын
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 6 ай бұрын
Awesome! I agree, makes life easier!
@MSLICENT
@MSLICENT 5 ай бұрын
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?
@weeqn1967
@weeqn1967 6 ай бұрын
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 6 ай бұрын
Awesome! See you next Sunday then!
@Santiago-bl3xu
@Santiago-bl3xu 6 ай бұрын
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 6 ай бұрын
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!
@Dev1gne
@Dev1gne 6 ай бұрын
Another awesome AI video after GOAP, already looking forward to coding along and learning something new!
@git-amend
@git-amend 6 ай бұрын
Great! Hope you like it!
@AndrewYakymetsLS
@AndrewYakymetsLS 6 ай бұрын
Thanks! This is exactly what I was looking for.
@git-amend
@git-amend 6 ай бұрын
Glad to hear it!
@정동우-n2x
@정동우-n2x 6 ай бұрын
I love It. it was what I needed the most. Thank you very much.
@git-amend
@git-amend 6 ай бұрын
Great! I'm glad to hear that! I'm sure you will like next week's video too.
@정동우-n2x
@정동우-n2x 6 ай бұрын
@@git-amend There's never been a time when I didn't like your video. However, this video is "LOVE".!!
@neetshonen
@neetshonen 6 ай бұрын
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 6 ай бұрын
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!
@P1casse
@P1casse 4 ай бұрын
Great video as always
@git-amend
@git-amend 4 ай бұрын
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.
@fleity
@fleity 6 ай бұрын
Probably has been asked a 1000 times before but anyway, which tool do you use to draw your diagrams?
@git-amend
@git-amend 6 ай бұрын
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: kzbin.info/www/bejne/pWHIqX5jrauDac0
@NomortaL
@NomortaL 28 күн бұрын
It appears that the current child keeps getting called until it returns Success or Failure. What if we want to run a higher priority node to be checked while the current node is still Running? For example, while the AI is running around. It's HP gets too low and now it needs to run away
@vic137
@vic137 4 ай бұрын
Woww. And here I am spending a month on if statements and switch cases to make my AI
@git-amend
@git-amend 4 ай бұрын
Nice, big improvement then.
@Kzyh43
@Kzyh43 6 ай бұрын
Thanks! Awesome as usual!
@git-amend
@git-amend 6 ай бұрын
Thanks again!
@BrandonLing-ko6hb
@BrandonLing-ko6hb 6 ай бұрын
amazing video yet again!
@git-amend
@git-amend 6 ай бұрын
Thank you!
@ninqbi1738
@ninqbi1738 5 ай бұрын
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 5 ай бұрын
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. kzbin.info/www/bejne/aXTTdqGVotKladE
@olegtopalov4120
@olegtopalov4120 6 ай бұрын
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 6 ай бұрын
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!
@LightcarrierStudios
@LightcarrierStudios 29 күн бұрын
Why is the processing logic different for the Selector and PrioritySelector? It seems they should be the same except for using a sorted list.
@git-amend
@git-amend 29 күн бұрын
The processing logic differs because `PrioritySelector` evaluates each child in priority order from the start on each tick, whereas `Selector` iterates through children sequentially, resuming where it left off. In `PrioritySelector`, resetting ensures high-priority children are always checked first, which is useful for dynamic priorities. `Selector`, by contrast, advances incrementally through its list of children, maintaining its position unless it needs to reset, which is simpler and efficient for sequential decision-making.
@lucianobisconti9078
@lucianobisconti9078 6 ай бұрын
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 6 ай бұрын
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 6 ай бұрын
Slowly but surely we'll cover all sorts of game architecture topics like this!
@TheKr0ckeR
@TheKr0ckeR 6 ай бұрын
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 6 ай бұрын
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 6 ай бұрын
@@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.
@-moristan109
@-moristan109 6 ай бұрын
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 6 ай бұрын
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!
@OskGame
@OskGame 6 ай бұрын
So great, keep it up!!!
@git-amend
@git-amend 6 ай бұрын
Thank you! Will do!
@OskGame
@OskGame 6 ай бұрын
@@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
@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!!!
@MarushiaDark316
@MarushiaDark316 6 ай бұрын
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 6 ай бұрын
Yes, you can apply a Fluent Builder pattern to this very easily.
@laserbean00001
@laserbean00001 6 ай бұрын
Do you have a video on menu systems yet?
@git-amend
@git-amend 6 ай бұрын
Last year I made one for UGUI that might interest you: kzbin.info/www/bejne/jZrVaJ2DpZqplc0
@Wanderlust_Duo
@Wanderlust_Duo 6 ай бұрын
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 6 ай бұрын
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 6 ай бұрын
@@git-amend ok, thanks, I'll check it:)
@joeb3017
@joeb3017 3 ай бұрын
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?
@Kzyh43
@Kzyh43 6 ай бұрын
Hey. I was wondering if UntilFail Node should return Success when the child returns Failure?
@git-amend
@git-amend 6 ай бұрын
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.
@gwm249
@gwm249 5 ай бұрын
What is the whiteboard app been used here in the intro?
@git-amend
@git-amend 5 ай бұрын
That's Excalidraw for Obsidian, though you can use it standalone too.
@omeryilmaz1021
@omeryilmaz1021 6 ай бұрын
Whats the benefits of implementing such a state management with strategy pattern instead of using standart abstraction of state machine
@git-amend
@git-amend 6 ай бұрын
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.
@falgunpawar
@falgunpawar 2 ай бұрын
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 2 ай бұрын
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 2 ай бұрын
@@git-amend okay, thank you so much!
@xbbotz866
@xbbotz866 6 ай бұрын
god-like tutorial
@git-amend
@git-amend 6 ай бұрын
Thanks!
@mertakbulut241
@mertakbulut241 6 ай бұрын
Thanks for the video sir. Can you do some video about "Custom Package Publishing" ?
@git-amend
@git-amend 6 ай бұрын
You are welcome. Yes, I have a video about creating packages coming soon!
@ragerungames
@ragerungames 6 ай бұрын
Awesome video... ❤
@git-amend
@git-amend 6 ай бұрын
Glad you liked it
@abraiyan7984
@abraiyan7984 6 ай бұрын
Thanks for this 🙏
@git-amend
@git-amend 6 ай бұрын
My pleasure 😊
@italoamaralnvcc
@italoamaralnvcc 6 ай бұрын
Amazing stuff! Quick question.... What do you use to make graphs like at 1:20? It seems very useful.
@git-amend
@git-amend 6 ай бұрын
Thanks! I use Excalidraw for Obsidian - can see that here: kzbin.info/www/bejne/pWHIqX5jrauDac0
@crazyfox55
@crazyfox55 6 ай бұрын
But what if I just put this in a big if statement?
@damonfedorick
@damonfedorick 6 ай бұрын
Nice
@git-amend
@git-amend 6 ай бұрын
Thanks
@paulushcgcj
@paulushcgcj 6 ай бұрын
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.
@SuperDude101
@SuperDude101 5 ай бұрын
Noice 👍
@git-amend
@git-amend 5 ай бұрын
Thank you!
@ranejeb
@ranejeb 6 ай бұрын
At first glance BahviorTree node and Sequence node look both the same...
@git-amend
@git-amend 6 ай бұрын
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
Рет қаралды 9 М.
Code Like a Pro: Refactoring to Patterns!
25:52
git-amend
Рет қаралды 6 М.
When u fight over the armrest
00:41
Adam W
Рет қаралды 32 МЛН
За кого болели?😂
00:18
МЯТНАЯ ФАНТА
Рет қаралды 3,2 МЛН
If people acted like cats 🙀😹 LeoNata family #shorts
00:22
LeoNata Family
Рет қаралды 21 МЛН
Which AI Behavior Framework Should You Use? | AI Series 46
17:26
LlamAcademy
Рет қаралды 50 М.
The Unity HACK that the PROS know
21:27
git-amend
Рет қаралды 17 М.
Service Locator: Inversion of Control in Unity C#
19:04
git-amend
Рет қаралды 12 М.
Build AI Behaviour Trees FAST in Unity with C#
23:12
Mina Pêcheux
Рет қаралды 83 М.
Expression Trees in Unity: The SECRET to Flexible Game Logic
14:50
How to use the Command Pattern (Skill Combos Example)
11:08
git-amend
Рет қаралды 10 М.
When to use Factory and Abstract Factory Programming Patterns
12:13
What is a Behavior Tree and How do they work? (BT intro part 1)
17:11
When u fight over the armrest
00:41
Adam W
Рет қаралды 32 МЛН