简体   繁体   English

Unity 2D-状态机

[英]Unity 2D - State Machine

I made a state machine to control my inputs and state animations. 我制作了一个状态机来控制我的输入和状态动画。 How can I use the following enum: 我如何使用以下枚举:

public enum mover_personagem
{
    mover_on,
    move_off
}

to activate and deactive input as follows: 激活和停用输入,如下所示:

private void Movimentar(float horizontal)
{
    if (move == mover_personagem.mover_on)
    {
        anim.SetFloat("speed", Mathf.Abs(Input.GetAxis("Horizontal")));

        myRigibody2D.velocity = new Vector2(
            horizontal * moveSpeed,
            myRigibody2D.velocity.y);
    }
}

If i use the state mover_off when a dialog box is open for the player stop walk animation, it's not working. 如果在为播放器停止行走动画打开对话框时使用状态mover_off ,则该行不通。

You can create a Class named Globals or whatever you want where you can declare all the static variables. 您可以创建一个名为Globals的类,也可以在需要的地方声明所有静态变量。

For example, you have created your enum and its variable in Globals.cs . 例如,您已经在Globals.cs创建了enum及其variable

public enum MOVER_STATE
{
    ON,
    OFF
}

public class Globals
{
    public static MOVER_STATE CURRENT_MOVER_STATE = MOVER_STATE.OFF; // Initial State
}

Now you can check it anywhere in any class, like 现在,您可以在任何班级的任何地方检查它,例如

switch(Globals.CURRENT_MOVER_STATE){
    case MOVER_STATE.OFF:
        //TODO: Do anything if Off
        break;
    case MOVER_STATE.ON:
        //TODO: Do anything if On
        break;
    }

You can also assign it anywhere. 您也可以在任何地方分配它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM