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

Поділитися
Вставка
  • Опубліковано 10 лип 2024
  • 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)
    ❤️ ua-cam.com/users/iHeartGameDev...
    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

КОМЕНТАРІ • 73

  • @iHeartGameDev
    @iHeartGameDev  5 місяців тому +17

    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 5 місяців тому +1

      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 5 місяців тому +2

      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 5 місяців тому +1

      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 4 місяці тому

      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 4 місяці тому

      @@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.

  • @Keymomentshorts
    @Keymomentshorts 2 місяці тому +1

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

  • @TheSoundWeaver
    @TheSoundWeaver 3 місяці тому

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

  • @bunggo9914
    @bunggo9914 5 місяців тому +10

    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  5 місяців тому +1

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

  • @suleimanshah5865
    @suleimanshah5865 3 місяці тому +1

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

  • @haihao3902
    @haihao3902 3 місяці тому

    A reflesh and replenish state machine, well done!

  • @BornToTroll-it5ju
    @BornToTroll-it5ju 2 місяці тому

    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.

  • @j_cap95
    @j_cap95 5 місяців тому +2

    Your content is gold dude

    • @iHeartGameDev
      @iHeartGameDev  5 місяців тому

      Thank you J!! Much appreciated!!

  • @ItzSylvan
    @ItzSylvan 5 місяців тому +4

    master explainer

  • @GameDevBox
    @GameDevBox 5 місяців тому +2

    Thanks man you are Wonderful! great explanation💙

    • @iHeartGameDev
      @iHeartGameDev  5 місяців тому +1

      Thank you for your kindness!! 💛

  • @brickch4pel
    @brickch4pel 4 місяці тому

    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.

  • @ItwasajokeVrballslol
    @ItwasajokeVrballslol 4 місяці тому

    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

  • @Hoptronics
    @Hoptronics 2 місяці тому

    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 .

  • @fnndxgh4028
    @fnndxgh4028 16 годин тому +1

    Thanks for the tutorial, but I have a question. For what reason do u create another local variable of Context in ResetState and so on if we already have the same thing in EnvironmentInteractionState as a protected to use?

  • @akitoakito
    @akitoakito 5 місяців тому +1

    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  5 місяців тому +1

      My interest in Godot is constantly growing!

  • @benjamindreyer9884
    @benjamindreyer9884 5 місяців тому +2

    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  5 місяців тому

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

  • @familyCarMusics
    @familyCarMusics 3 місяці тому

    waiting for next video....!

  • @user-do2wy1zd4b
    @user-do2wy1zd4b 5 місяців тому +3

    waiting

  • @craigormsby458
    @craigormsby458 5 місяців тому

    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

  • @bogumiwyrwa216
    @bogumiwyrwa216 2 місяці тому

    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;

  • @thefirebeanie5481
    @thefirebeanie5481 16 днів тому

    do you have this tutorial for player movment? its kind of hard to work backwards using a past tutorial for this state machine but the tutorial for this is animation not movement, thanks!

  • @GameCheeseHD
    @GameCheeseHD 4 місяці тому

    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!

  • @4f00d
    @4f00d 5 місяців тому

    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..

  • @wjk3021
    @wjk3021 3 місяці тому

    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)

  • @saus80085
    @saus80085 4 місяці тому

    when will the next episode come out?

  • @lemapp
    @lemapp 5 місяців тому +2

    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  5 місяців тому +1

      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

  • @Noob-uk8gy
    @Noob-uk8gy 5 місяців тому +2

    Hype

  • @TheRubenazo
    @TheRubenazo 3 місяці тому

    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

  • @JitendraKumar-xv4kt
    @JitendraKumar-xv4kt 2 місяці тому

    When will you upload the final implementation video ?

    • @iHeartGameDev
      @iHeartGameDev  2 місяці тому +1

      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 2 місяці тому

      @@iHeartGameDev Waiting

  • @YGDEV69
    @YGDEV69 3 місяці тому

    Samyam's brother 😂

  • @icefox2853
    @icefox2853 4 місяці тому

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

    • @asfandyarriaz3396
      @asfandyarriaz3396 3 місяці тому +1

      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 3 місяці тому

      @@asfandyarriaz3396thanks!

    • @icefox2853
      @icefox2853 2 місяці тому

      @@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 2 місяці тому

      @@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.

  • @icefox2853
    @icefox2853 4 місяці тому

    HOW to switch states??????

  • @PacVan12
    @PacVan12 3 місяці тому

    I don't understand anything

  • @etrex5272
    @etrex5272 5 місяців тому +8

    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  5 місяців тому +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 5 місяців тому +2

      @@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  5 місяців тому +6

      @@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
      ua-cam.com/video/Vt8aZDPzRjI/v-deo.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 5 місяців тому +2

      @@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  5 місяців тому +1

      @@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_uzucracks1554 4 місяці тому +1

    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  4 місяці тому

      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 4 місяці тому

      @@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 4 місяці тому

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