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

Can anyone explain why this isn't working? FSM fail.

Discussion in 'Scripting' started by cristo, Oct 2, 2016.

  1. cristo

    cristo

    Joined:
    Dec 31, 2013
    Posts:
    265
    Hi people,

    I'm trying to make a cube move (and in one case rotate) to three different vector3s using a FSM.
    The cube begins to hit the first state and freezes.

    Any feedback would be great.


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class FSMVersion : MonoBehaviour {
    5.  
    6.  
    7.     GameObject player;
    8.    
    9.     private float Spin= 3.0f;
    10.     private float moveSpeed = 6.0f;
    11.     new Vector3 Goal1 = new Vector3(3, 5, 0);
    12.     new Vector3 Goal2 = new Vector3(3, 0, 30);
    13.     new Vector3 Goal3 = new Vector3(-3, 3, 0);
    14.  
    15.     public enum State
    16.  
    17.     {
    18.         One,
    19.         Two,
    20.         Three
    21.     }
    22.  
    23.  
    24.  
    25.     // _state is an empty initial state needed to hold and run the other states.
    26.  
    27.     public State _state;
    28.     // Use this for initialization
    29.     void Start()
    30.     {
    31.         GetComponent<Transform>();
    32.         transform.rotation = Quaternion.Euler(0, 0, 0);
    33.         player = gameObject;
    34.      
    35.          }
    36.     void Update (){
    37.  
    38.  
    39.         switch (_state)
    40.             {
    41.  
    42.                 case State.One:
    43.                     Onee();
    44.                     break;
    45.                 case State.Two:
    46.                     Twoo();
    47.                     break;
    48.                 case State.Three:
    49.                     Threee();
    50.                     break;
    51.             }
    52.  
    53.         }
    54.  
    55.     // Update is called once per frame
    56.  
    57.           public void Onee()
    58.     {
    59.         Debug.Log ("One");
    60.         player.transform.position = Vector3.MoveTowards(player.transform.position, Goal1, moveSpeed * Time.deltaTime);
    61.    
    62.         if (player.transform.position == Goal1) {
    63.             Twoo();
    64.         }
    65.     }
    66.     public void Twoo()
    67.     {
    68.         Debug.Log("Two");
    69.         player.transform.LookAt(Goal2);
    70.         player.transform.position = Vector3.MoveTowards(player.transform.position, Goal2, moveSpeed * Time.deltaTime);
    71.         //player.transform.Rotate(player.transform.right * Spin * Time.deltaTime);
    72.         transform.Rotate(new Vector3(1, 0, 0) * Spin * Time.deltaTime);
    73.  
    74.         if (player.transform.position == Goal2) {
    75.             Threee();
    76.         } }
    77.  
    78.     public void Threee()
    79.     {
    80.         Debug.Log("Three");
    81.         player.transform.position = Vector3.MoveTowards (player.transform.position, Goal3, moveSpeed * Time.deltaTime);
    82.     }
    83. }
    84.  
     
  2. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    You shouldn't Call Twoo from Onee. You should change the "State" of your state machine at the end of Onee. That way in Update, it starts calling Twoo:

    Change this:
    Code (CSharp):
    1. if (player.transform.position == Goal1) {
    2.             Twoo();
    3.         }
    To:
    Code (CSharp):
    1. if (player.transform.position == Goal1) {
    2.             _state = State.Two;
    3.         }
    And make the Same Changes at the end of Twoo to change the state to State.Three
     
    cristo likes this.
  3. cristo

    cristo

    Joined:
    Dec 31, 2013
    Posts:
    265
    So happy. That was a massive hole in my education corrected.