How to Program in Unity: Using my "BETTER" State Machine for Procedural Animation

Learn how to program a custom state machine in C# and Unity in this brand new tutorial explaining the state machine I used for procedural animation!
This tutorial explains how you can use the reusable components I've shared previously in the "Programming a BETTER state machine" video. Together, we'll walk through the code to see just how powerful a well-structured state machine can be!
This video is also a continuation of the procedural animation and environment interaction series on the channel. We'll build upon this state machine to bring our character to life!
SUPPORT THE CHANNEL AND GET EXCLUSIVE PERKS:
💛 / iheartgamedev (Project Files Available)
❤️ kzread.info...
WANT MORE?
Interested in learning more about animating characters in Unity? Check out my growing series of tutorials:
✅ • Programming For Produc...
✦ Like the vid? Please consider Subscribing!
bit.ly/2YdIb6j
✦ Missed out on the last episode?
• How to Move Characters...
SOCIAL:
✦ Discord
/ discord
✦ Twitter
/ iheartgamedev
►TIMESTAMPS:
Intro: 0:00
Concrete State Machine Setup: 0:43
Abstract Middle-Layer: 3:57
Sharing data between states: 5:30
Concrete States Setup: 8:18
Question to the community: 11:00
#unity3d #designpatterns #gamedev

Пікірлер: 71

  • @bunggo9914
    @bunggo99143 ай бұрын

    I'm so hyped for the next episode! I really appreciate your efforts on making high quality videos. I am currently implementing a state machine in which I'm using scriptable objects instead of enums or C# classes for the states, and your videos has been vital for my understanding of how state machine works and has been a great point of reference for my own implementation.

  • @iHeartGameDev

    @iHeartGameDev

    3 ай бұрын

    Heck yeah - I’m curious if you see any downside in using a scriptable object versus c# classes?

  • @iHeartGameDev
    @iHeartGameDev3 ай бұрын

    Hey everyone! Really excited to share this tutorial with you, but I do have a question! What other tutorials are you interested in outside of this procedural animation series? Would you be interested in more Unity tutorials, should I branch into Godot tutorials? Let me know by leaving a comment! And if you enjoyed this video and want to help it reach more people, liking the video would 100% help the algorithm and engagement.

  • @GameDevBox

    @GameDevBox

    3 ай бұрын

    I really love to see a breakdown of the Animations of Some games like Inside or Sons of the Forest or Little Nightmares It will be so cool if you show more games like you did with Uncharted.

  • @nwabugwudumebi

    @nwabugwudumebi

    3 ай бұрын

    I would really love to see you start a beginner tutorial on shader graph, VFX graph and the particle system in unity and how you can implement them In games

  • @stephenq24

    @stephenq24

    3 ай бұрын

    I like your videos. The video quality is good and you explain things well. I believe that you and your channel have a LOT of potential. If you are truly serious about growing this channel, I HIGHLY recommend doing a tutorial series that focuses on a single game project from beginning to end. It is critical that in this series you focus on 1.) best practices 2.) reusable SYSTEMS (like this better state machine) and 3.) scalability. If you do that, I think your channel will grow.

  • @HaywirePhoenix

    @HaywirePhoenix

    3 ай бұрын

    On other engines, I'd love to see you try Stride as it's so similar to Unity's syntax and UI. As its open source I may give it a try after >10 years in unity.

  • @weckar

    @weckar

    3 ай бұрын

    @@stephenq24 That seems the exact opposite of what they should do. With bite-sized tutorials, every video can be a jump-on point. With a long series on a single project it's all or nothing.

  • @BornToTroll-it5ju
    @BornToTroll-it5juАй бұрын

    i still don't get why your channel isn't right up there with the best Unity tutorial spots on the web. Flawless presentation, easy to follow. The quality of each upload just drips from the screen. Honestly the boys at Unity should hire you on the spot esp now that Brackeys has gone turncoat and joined the Godot dark side.

  • @baptista6400
    @baptista640025 күн бұрын

    I'm loving this series, I'm learning a lot about state machines, I can't wait for the next episode

  • @Hoptronics
    @Hoptronics25 күн бұрын

    Dude I'm ready.. I'm hoping I figure it out before your next video. It'd be interesting to see how i do it vs you .

  • @TheSoundWeaver
    @TheSoundWeaverАй бұрын

    Amazing tutorial with a clear and great explanations in each step. Thank you! Waiting for the next episode)

  • @suleimanshah5865
    @suleimanshah58652 ай бұрын

    ❤❤ Love it i was looking for it andd trying to approach this kind of solution.. thanks waiting for next update

  • @haihao3902
    @haihao39022 ай бұрын

    A reflesh and replenish state machine, well done!

  • @ItzSylvan
    @ItzSylvan3 ай бұрын

    master explainer

  • @GameDevBox
    @GameDevBox3 ай бұрын

    Thanks man you are Wonderful! great explanation💙

  • @iHeartGameDev

    @iHeartGameDev

    3 ай бұрын

    Thank you for your kindness!! 💛

  • @j_cap95
    @j_cap953 ай бұрын

    Your content is gold dude

  • @iHeartGameDev

    @iHeartGameDev

    3 ай бұрын

    Thank you J!! Much appreciated!!

  • @ItwasajokeVrballslol
    @ItwasajokeVrballslol3 ай бұрын

    I really hope you can get around to full movement animation using procedural animation, more and more games are implementing it, and it looks CRISP, Cheers

  • @benjamindreyer9884
    @benjamindreyer98843 ай бұрын

    This content is really great! Do you have any plans for using the hierarchical state machine for animations? I am curious as to how that would be implemented:)

  • @iHeartGameDev

    @iHeartGameDev

    3 ай бұрын

    Hey that sounds intense! I do love myself a hierarchical state machine!

  • @brickch4pel
    @brickch4pel3 ай бұрын

    Something I've desperately been looking for a tutorial/breakdown on is procedural hit impacts; Something like what you see in Resident Evil 2 remake, where shooting individual parts of an enemy will have them react to that particular part in a natural way.

  • @craigormsby458
    @craigormsby4583 ай бұрын

    So do you expect each state to decide what the next state will be? If so this can get pretty hard to follow when the fsm gets more complicated as you have to dig through the code to work out what wilk happen. I would suggest you look at the paradigm used by stateless (its a generic c# fsm - you can look it up). It offers significant advantages for code reuse and has a transition map that makes the code easier to follow. I have written fsms similar to yours before but i found the stateless pattern created better more readable code. I did not use Stateless directly as i wanted each state called in the the update loop but its design is excellent. Btw, you might find that you can use reflection to create the states and populate the dictionary when initialising. That is what i did with my version of yours

  • @akitoakito
    @akitoakito3 ай бұрын

    Godot 4 is nice but think you should do a community poll to see if people would like a "getting started" or a "how to x" in godot. Thanks for another great video.

  • @iHeartGameDev

    @iHeartGameDev

    3 ай бұрын

    My interest in Godot is constantly growing!

  • @familyCarMusics
    @familyCarMusics2 ай бұрын

    waiting for next video....!

  • @bogumiwyrwa216
    @bogumiwyrwa21621 күн бұрын

    Hello, Greate tutorial. I have some questions: Why in class ResetState you add line in constructor: EnvironmentInteractionContext Context = context; ? EnvironmentInteractionState class has constructor with Context = context;

  • @4f00d
    @4f00d3 ай бұрын

    great tutorials, but what if we have spawn-able Character(not present in Scene, but after sometime) and pickable weapons not under Characters hierarchy at start, searched forums and no luck, animation rigging breaks 😆 tried to use methods from Jobs and few from animation rigging refresh/rebuild( ) the Rig, but no luck..

  • @GameCheeseHD
    @GameCheeseHD2 ай бұрын

    Hey, I've just been setting up this state machine in my project. I've run into a little snag, I want to check what the current state is but I can't figure out how to do it. For example, using your script and variable names, if I wanted to check if CurrentState is the Search state, what's the correct way of going about this? Essentially, I want to be able to do something like the following: if (EnvironmentInteractionStateMachine.CurrentState == EEnvironmentInteractionState.Search) { // do code } Does that make sense? I tried writing a few functions of my own to try to return the right value but I always seem to get type mismatches and I'm at a loss for now. Any help would be super appreciated!

  • @wjk3021
    @wjk30212 ай бұрын

    Hey, those state machines are running in only main thread. You should use jobs and sync points to make it multithread-able if you use mono. Data oriented way is better to implement HSMs because the states are changed by the data and it can handle millions of entities that have same state machines (i.e. using jobs and command buffers in one system or using one system for one state)

  • @user-do2wy1zd4b
    @user-do2wy1zd4b3 ай бұрын

    waiting

  • @iHeartGameDev

    @iHeartGameDev

    3 ай бұрын

    See you Wednesday!

  • @JitendraKumar-xv4kt
    @JitendraKumar-xv4kt28 күн бұрын

    When will you upload the final implementation video ?

  • @iHeartGameDev

    @iHeartGameDev

    28 күн бұрын

    I'm almost done withe the next part (will be about a 20 minute video), but there are two more after that

  • @JitendraKumar-xv4kt

    @JitendraKumar-xv4kt

    26 күн бұрын

    @@iHeartGameDev Waiting

  • @saus80085
    @saus800852 ай бұрын

    when will the next episode come out?

  • @lemapp
    @lemapp3 ай бұрын

    My IDE (Rider) complained about 'using UnityEngine.Animations.Rigging;' . Under Package Manager, I went to Unity Registry and located Animation Rigging. I added this to my project and my IDE no longer complained.

  • @iHeartGameDev

    @iHeartGameDev

    3 ай бұрын

    Yes that part is definitely necessary if you don’t have it installed. Apologies, this video is a part of a larger series around procedural animation

  • @TheRubenazo
    @TheRubenazoАй бұрын

    Hi, how do I make it so I change the state from one to the other. Do I just change the state key?

  • @EvilMrPape

    @EvilMrPape

    Күн бұрын

    I am wondering the same thing. I tried it, and yes, somehow it changes the status, but the transition method is not called.

  • @EvilMrPape

    @EvilMrPape

    Күн бұрын

    I found the solution here in the comments. Write the condition for the status change in the GetNextState() function of the status you want to change from. @asfandyarriaz3396

  • @Noob-uk8gy
    @Noob-uk8gy3 ай бұрын

    Hype

  • @YGDEV69
    @YGDEV692 ай бұрын

    Samyam's brother 😂

  • @icefox2853
    @icefox28533 ай бұрын

    Well, how to switch btw states???????

  • @asfandyarriaz3396

    @asfandyarriaz3396

    2 ай бұрын

    you can switch states from GetNextState() like this public override PlayerStateMachine.EPlayerState GetNextState() { if(Context.CharacterController.isGrounded) { return PlayerStateMachine.EPlayerState.Grounded; } return StateKey; } I added a conditional statement, GetNextState() is called in UpdateState() method, if my condition is met, the state is switched and the code handles the rest.

  • @icefox2853

    @icefox2853

    2 ай бұрын

    @@asfandyarriaz3396thanks!

  • @icefox2853

    @icefox2853

    Ай бұрын

    @@asfandyarriaz3396 Huge thanks! This topic is totaly hard to understand and it took me about several months to realize what the heck is going on.

  • @icefox2853

    @icefox2853

    Ай бұрын

    @@asfandyarriaz3396 And i cant understand why he set methods in BaseState abstract. I think it is better to set GetNextState as abstract and other methods as virtual because some states may not need them.

  • @EvilMrPape

    @EvilMrPape

    Күн бұрын

    @@asfandyarriaz3396 Thank you, thank you, thank you.

  • @PacVan12
    @PacVan122 ай бұрын

    I don't understand anything

  • @icefox2853
    @icefox28533 ай бұрын

    HOW to switch states??????

  • @etrex5272
    @etrex52723 ай бұрын

    A quick look at this and I have almost no clue whats going on, its near unreadable to me. Multiple layers of inheritance and too similiar naming convention that are also extremely long. For what? I hope new developers dont get discouraged by this. I work on an arpg which has tons of systems created from scratch without even being close to using a spaghetti like this. I'm actually confused.... what are we even trying to achieve here?

  • @iHeartGameDev

    @iHeartGameDev

    3 ай бұрын

    Hey there, happy to talk through the layers of inheritance. It’s mentioned at 5:00 min in, but the purpose of my original base state and state manager classes are to be reused when I make more state machines in this or other projects. They include the fundamental pieces of any state machine. So the middle layer is designed to serve as the base state for a particular state machine, inheriting from base state and adding any logic that would be repeated between the concrete states but wouldn’t be included in a different state machine. Would be curious if you’d handle this differently when attempting to design reusable state machine components and then creating new state machines based off those as we’re doing here.

  • @etrex5272

    @etrex5272

    3 ай бұрын

    @@iHeartGameDev yea I understand what you are doing, and it probably works as it should. Its just the complete opposite of what I want in a system. I'm no expert, but speaking for myself I would look for any opportunity to simplify it further and remove both layers and scripts. How? Not sure, my brain is at max load simplifying own systems. I just finished a 10 hour work session and stumbled accross this randomly and really wanted to put in a word for those that might be discouraged by it. I dont have the capacity rn to think of ways to improve this. But ill post it here if my curiosity gets the better of me or I create a reusable statemachine in the future.

  • @iHeartGameDev

    @iHeartGameDev

    3 ай бұрын

    @@etrex5272 got it, that’s understandable. For what it’s worth, I cover state machines quite a bit on this channel, and the first video I made was intended for beginners. That’s this one here: How to Program in Unity: State Machines Explained kzread.info/dash/bejne/iKhsw7x9gN24mqw.html As I’ve continued my own journey, my videos are less targeted towards beginners. That being said, I’m not 100% sold on my own implementation and am always looking for ways it can be improved. I’m also interested in alternative ways to program them entirely from my own implementation. So if you do end up making your own, I would be interested in seeing it! Cheers, Nicky

  • @etrex5272

    @etrex5272

    3 ай бұрын

    @@iHeartGameDev great mindset man, and very wholesome responses. Now I feel bad about my kinda grumpy first comment allthough there was some truth to it. Hehe. Yea, systems like these can almost always be improved, and thats what makes it interesting and fun to work with. Also sometimes mind-blowing when you see how much code can be simplified. I removed three scripts and several enums this week and replaced them with logic based around _1_ boolean instead... 😂 truly beautiful!

  • @iHeartGameDev

    @iHeartGameDev

    3 ай бұрын

    @@etrex5272 well said and all good! Programming is certainly an opinionated profession so I’m used to differing opinions after all these years. Still trying to approach alternative approaches with an open mind. That’s how I learn best: from people better at it than me. 😆

  • @xaruto_uzucracks1554
    @xaruto_uzucracks15542 ай бұрын

    how does this makes sense? void Update() { EState nextStateKey = CurrentState.GetNextState(); if (!IsTransitioningState && nextStateKey.Equals(CurrentState.StateKey)) CurrentState.UpdateState(); else if (!IsTransitioningState) TransitionToState(nextStateKey); when the GetNextState returns the key MovementStateMachine.EMovementState GetNextState() { return StateKey; } if I just change the CurrentState = States[ESomeState.OtherState]; because then, it would reference to its own StateKey nextstatekey would be the same as currentstate.StateKey everytime no? i'm really going insane over here

  • @iHeartGameDev

    @iHeartGameDev

    2 ай бұрын

    Correct! That is what will currently happen. However, for each state we'll be adding conditional logic where we don't want to return that default value: For example in the SearchState: GetNextState returns the key MovementStateMachine.EMovementState GetNextState() { if (ctx.jumpPressed) { return MovementStateMachine.JUMP } return StateKey; }

  • @xaruto_uzucracks1554

    @xaruto_uzucracks1554

    2 ай бұрын

    @@iHeartGameDev I think I kinda understand a little bit more now thanks, but still kinda cloudy like the context of jumpPressed is accurate im really anxious for the next video

  • @Brogox

    @Brogox

    2 ай бұрын

    @@iHeartGameDevThanks. I just needed this info. I was just wondering how to use GetNextState