Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Simple State machine, its not working

Discussion in 'Scripting' started by martur94, Dec 22, 2015.

  1. martur94

    martur94

    Joined:
    Dec 22, 2015
    Posts:
    17
    I made a state machine to control my inputs and state animations. How can I use the following enum:

    Code (CSharp):
    1. public enum mover_personagem
    2. {
    3.     mover_on,
    4.     move_off
    5. }
    to activate and deactive input as follows:

    Code (CSharp):
    1.  
    2. private void Movimentar(float horizontal)
    3. {
    4. if(move == mover_personagem.mover_on)
    5. {
    6.    anim.SetFloat("speed",Mathf.Abs(Input.GetAxis("Horizontal")));
    7. myRigibody2D.velocity =newVector2(
    8. horizontal * moveSpeed,
    9. myRigibody2D.velocity.y);
    10. }
    11. }
    12.  
    If i use the state mover_off when a dialog box is open for the player stop walk animation, it's not working.

    What i do it ?
     
    Last edited: Dec 22, 2015
  2. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,533
    Try this:
    Code (CSharp):
    1. public enum mover_personagem
    2. {
    3.     mover_on,
    4.     move_off
    5. }
    6.  
    7. private void Movimentar(float horizontal)
    8. {
    9.     switch (move)
    10.     {
    11.     case mover_personagem.mover_on:
    12.         anim.SetFloat("speed",Mathf.Abs(Input.GetAxis("Horizontal")));
    13.         myRigibody2D.velocity =newVector2(horizontal * moveSpeed,myRigibody2D.velocity.y);
    14.         break;
    15.     case mover_personagem.move_off:
    16.         anim.SetFloat("speed",0);
    17.         break;
    18.     default:
    19.         Debug.LogError("Unknown state: " + move);
    20.         break;
    21.     }
    22. }
    23.  
    24. // Every frame, update movement:
    25. void Update()
    26. {
    27.     Movimentar(horizontal);
    28. }
    29.  
    30. // Assign to OnClick handlers for UI buttons that open and close dialog box:
    31. public void OnDialogBox(bool open)
    32. {
    33.     move = open ? mover_personagem.move_off : mover_personagem.mover_on;
    34. }
    It should be inside your MonoBehaviour script.
     
  3. martur94

    martur94

    Joined:
    Dec 22, 2015
    Posts:
    17
    That method:
    Code (CSharp):
    1. public void OnDialogBox(bool open)
    2. {
    3.     move = open ? mover_personagem.move_off : mover_personagem.mover_on;
    4. }
    is on other class all right ?
     
  4. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,533
    Yes, as long as it sets the "move" variable in the same script as Update and Movimentar.
     
  5. martur94

    martur94

    Joined:
    Dec 22, 2015
    Posts:
    17
    Ok. But i need, if the player enter on a trigger event and input key 'J', its open a Panel with a text.
    What i read about Enum, the states are statics.

    More on my situation its not working static enum

    Code (CSharp):
    1. private void Movimentar(float horizontal)
    2. {
    3.     switch (move)
    4.     {
    5.     case mover_personagem.mover_on:
    6.         anim.SetFloat("speed",Mathf.Abs(Input.GetAxis("Horizontal")));
    7.         myRigibody2D.velocity =newVector2(horizontal * moveSpeed,myRigibody2D.velocity.y);
    8.         break;
    9.     case mover_personagem.move_off:
    10.         anim.SetFloat("speed",0);
    11.         break;
    12.     default:
    13.         Debug.LogError("Unknown state: " + move);
    14.         break;
    15.     }
    16. }
    DialogueBox script:

    Code (CSharp):
    1. void Start () {
    2.  
    3.         player = FindObjectOfType<Movimento>();
    4.      
    5.  
    6.  
    7.         if (textFile != null)
    8.         {
    9.             textLines = (textFile.text.Split('\n'));
    10.         }
    11.        
    12.  
    13.         if (endLine == 0)
    14.         {
    15.             endLine = textLines.Length - 1;
    16.         }
    17.  
    18.     }
    19.  
    20.     void Update()
    21.     {
    22.  
    23.         caixaDialogo();
    24.  
    25.     }
    26.  
    27.     void caixaDialogo()
    28.     {
    29.         if(move == mover_personagem.off)
    30.         {
    31.                stopPlayermove = false;
    32.         texto.text = textLines[currentLine];
    33.         if (Input.GetKeyDown(KeyCode.Return))
    34.         {
    35.             currentLine += 1;
    36.         }
    37.  
    38.         if (currentLine > endLine)
    39.         {
    40.             DisableDialogueBox();
    41.             currentLine = 0;
    42.             move = mover_personagem.on;
    43.         }
    44.         }
    45.  
    46.     }
    and TriggerEvent script:

    Code (CSharp):
    1.     void OnTriggerEnter2D(Collider2D coll)
    2.     {
    3.         pressinado = true;
    4.         if (coll.name == "Player")
    5.         {
    6.             move = mover_personagem.off;
    7.         }
    8.     }