banner



Unity Use Animator As State Machine For Player?

Animator Controller FSM and Land Machine Behaviors

State Design Pattern

The State Design is close to a finite land auto. The State Pattern improves the FSM past making it more object oriented and more homo readable. The Context is the country car itself and and then you have the state that we can be in, each state is information technology's own class.

In their volume, Pattern Patterns: Elements of Reusable Object-Oriented Software, Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides — the Gang of Four, define the intent for the State pattern equally follows:

"Allow an object to modify its behavior when its internal state changes. The object will appear to change its class."

Definitions

  • Context — Maintains an Instance of a concrete country as the current state. This is the Country Automobile itself.
  • Abstract Land (State) — Defines an Interface which encapsulates all country-specific behaviors. Typically an Interface but can exist an Abstract Class or A Base Grade.
  • Physical State — Implements behaviors specific to a particular state of context. A Sub Class of the Abstract State.

Implementation

I am going to Take a look at a Locomotion State, This is the same ane I used in the FSM commodity.

Pure Code.

The first thing I am going to implement is the Context. I am going to utilise an Interface so there is not circular references when creating the state. I am saying that the Locomotion Context must have a method Chosen Set State that receives a parameter of the State Interface.

Locomotion Context Interface

Next I create the Locomotion Abstract State, again I volition exist using an Interface for this. Typically States have an Entry, an Exit, and Update Logic Methods. You can add whatever type of Method here that you want all states to have in mutual like Update Physics.

Now I can finish Implementing the Context. This contains the current state that we are in. When setting the state to a diffrent state I get out the current state, gear up the current state to the new state and I call the enter state. In the Update method I call the current states update method.

Locomotion Context

Now I can create any States that I want to be able to alter to, and put the logic in the State itself.

Grounded State

Crouching State

In Air State

This is much cleaner than a elementary FSM. If I want to add a new state, say pond state all I need to do is create that State, put the Update logic for swimming and the only states that I need to change are the states that I want to transition into the pond state, I can hands find those states and add the new logic.

Using Animator Component.

This can exist simplified even further by using Unity'due south built in Animator Component for the Context and Country Machine Behavior (Unity uses British spelling Behaviour) as the Abstract State.

Using these together I do not accept to brand sure to call Enter, Exit, or Update Methods on us. Unit handles all of this for me. It also handles some other Messages that you may discover useful for.

The first step is to set up the Animator Controller as My FSM

I so Add a beliefs to us, I can add together one that I have already created or add one that I have already created. I tin can besides have a State without a behavior. In this Example I will have a beliefs for all of my states.

Now all I have to do is Create my Concrete States (State Machine Behavior's) for my states. I then Override the Enter and Update Methods. There are several choices for Inbound, Exiting, and Updating use the ones that you need. The big one to be enlightened of is the difference between OnStateMachine(Enter/Exit) vs OnState(Enter/Exit). Of form you likewise have Ik and Move methods also.

  • On Land Machine Enter — Called on first Update frame when making a transition to a state automobile. This is non called when making a transition into a state auto sub-state.
  • On Country Machine Exit— Called on first Update frame when making a transition out of a state motorcar. This is not chosen when making a transition into a country machine sub-state.
  • On State Enter — Called on the first Update frame when a country machine evaluate this state.
  • On State Exit— Called on the final update frame when a state machine evaluate this state.

Creating Grounded Behavior

Crouching State Behavior.

In Air State Behavior

Since My grounded State and Crouching Country is the exact same code I could use the Grounded Beliefs for both. The Difference is when they transition States and that is controlled by the Animator Controller.

You tin also add more than and so i Behavior to each State, for instance instead of having a Grounded Country I take a movement State that changes state based on a Speed Variable I tin can place the Grounded Behavior on both The movement country and Crouching State, since both of these states will merely happen when the Character is Grounded. You and then place a Movement Behavior.

Real World Example.

I have a grapheme in my Game that has an Animator Controller fastened and a character movement script.

A Graphic symbol in my Game.

The Animator Controller consists two Land Machine, Grounded and In Air. The Ground state machine has a Behavior attached to it and the Jump State has a Behavior fastened to it.

Animator Controller

The Grounded Behavior sets the y velocity to exist a set up value when it enters the land.

Player Grounded Behavior

The Bound State Beliefs controls which bound the character uses, how high it jumps, which direction it is moving on the y access. It resets things if it enter the state and when it exits the country.

Player Jump Land Beliefs

The Character Movement moves based on the inputs information technology is given, information technology gets information technology'due south Y velocity from the animator state machine and it tells the country machine if the grapheme is grounded or non by setting the is grounded boolean.

Grapheme Motion Script work in progress.

Conclusion

The State Pattern makes it easy to implement complex state machines that can easily add new States.

Properly Implemented this likewise follows all of the SOILD principles. In the Case lawmaking I did technically pause the Interface Segregation Principle by having the On Leave and On Enter methods in the interface when none of my States really used them, if I was fully implementing them in a game they would probably have some sort of ready and clean up code that is needed then the side by side time the state ran it would have proper local values. The private States (Grounded, In Air, Crouching) tin peradventure interruption the Open-Closed Principle equally adding new States you may have to modify those classes in order to make sure the new transitions are added, there are several means this can be solved, the easiest of which is to use the blitheness system Unity has built in.

The congenital in Animator Controller is in reality using the Land Machine Pattern. The Animator Controller is the Context and the States that are added to information technology are the Concrete States that are using an Abstract State, there are 3 different types of states and a Sub State Auto that be added to the Country Motorcar. The dissimilar States that the controller is in can be farther extended by using Land Machine Behaviors.You lot can accept multiple types of characters in your game that use this same Animator Controller, even if the animations on the characters are vastly unlike (use an Override Controller).

The Lawmaking for this project can exist found on My Git Hub page.

https://github.com/JamesLaFritz/Design-Patterns-For-Unity/tree/release-StateMachines

Source: https://blog.devgenius.io/state-pattern-in-unity-cd180f98c45

Posted by: davisvien1961.blogspot.com

0 Response to "Unity Use Animator As State Machine For Player?"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel