Home > other >  Transfer: senior Unity, the state machine, the finite state machine
Transfer: senior Unity, the state machine, the finite state machine

Time:09-23

senior Unity, the state machine, the finite state machine http://blog.csdn.net/ios_song/article/details/52327417

One, the state machine
The state machine can separate logic code, increasing the maintainability and reusability of the code

We are going to learn this lesson, the use of state machine, in the Unity to implement some logical division, first we need to know, why the development of time using a state machine?
Before the development experience of friends, may know, in the application development framework is one of the most commonly used MVC framework,
Model view controller is the framework, but in game development, we will find it hard to apply MVC framework,
Because our game development, it doesn't, like website, or any other application development model, view and controller in the process of game development, their boundaries is fuzzy, the division is not very clear, this is because we have different types of games, the game itself underlying architecture will differ very far! And in the game, different game objects tend to have a lot of interaction of this kind of phenomenon,
So, this kind of vertical MVC framework is not suitable for game development, but I can still in the game development UI part use the MVC framework,
In addition to the UI, we also need some kind of degree to separate some program code of the game, for instance in the game we need some of the role of a script to control some logic, character movement, the properties such as attack, we can't finish the code in a script, we must through a certain method, put them into multiple scripts or multiple parts, in order to implement, because it divides the complex code into a a small part to achieve, we can improve the maintainability of the code, and reuse, which is why we want to use the state machine!
State machine can help us separate logic code,



Two, finite state machine
Finite state machine (FSM) referred to as the state machine
The state of FSM is a limited number of

Finite state machine is to show a finite number of state, and the state transition behavior of a model directly, in the development process of the game, the finite state machine to a complex function, or complex logic is simplified to a number of stable state! Later, in a state of these events on judgment,

Take an example: characters, for example
Characters, we use the state machine to implement the characters of complex logic, we will first according to our game or function, to put the game characters are divided into different status, such as the characters in the idle state, may also is in a state of attack, likely is in a state of walking, that is in a state of copy, transition task condition etc...

We put these logic code are divided according to the state, we are alone in a certain state, to achieve a particular code, if the game is divided into a combat role, we will fight as long as the relevant code in combat to be realized, certainly can't go into the state of the walk,

This is the use of state machine to help us logic code,

Example: the light bulb state diagram of the



Light bulb is divided into two states: on or off, at any time, bulb or is in a state of open, or is in a state of closed, if our current bulb is open, if we want to turn off the light bulb, we only need to close the switch, on the other hand, if our current bulb is closed, we want to open the light bulb, you just need to turn on the switch,

In the light bulb in the State diagram we can classify the two states, where the State (State) is to store the light bulb some information or perform some action,


If we switch from the open state mark the status of the intermediate is called state Transition (Transition)


State transition is used to describe: direct conversion,
We close the switch and turn on the switch's state transition condition, their function is used to trigger a state transition, we called the transition Event (Event)!


We are in a state of on or off, there will be an Action, the Action is likely to be carried out or instant execution, called Action (Action)!


Images of all the elements together, it is a state machine!
Three, light bulbs will return to the Unity of demo - this function in Unity to implement the
Prepared some material in order to project the ball first, and then we began to


Create a Plane in the scene, and then we put the Plane dyed green, then in the creation, to create a Cube, give Cube to dye a purple, then we can adjust the camera, on the Cube... To be continued...

CodePudding user response:

Is boring, is there any project case, the code on the right,
  • Related