Quantcast
Viewing latest article 22
Browse Latest Browse All 42

State Machine Tutorial Question

Hello, I am pretty inexperienced with coding but I am trying to create a simple state machine. I followed a step-by-step youtube tutorial, but I am having issues. When I run the scene, I asked the statemachine script to output the current start to the debug log but in my debug log I get both "no current state" and "legs". It seems that because the state is being read as "null" I can't enter a state and update the logic and transition to another state. I'm pretty confused, can anyone let me know what I'm doing wrong?? Thanks so much! Tutorial: https://www.youtube.com/watch?v=-VkezxxjsSE&t=1288s StateMachine: using System.Collections; using System.Collections.Generic; using UnityEngine; public class StateMachine : MonoBehaviour { BaseState currentState; void Start() { currentState = GetInitialState(); if (currentState != null) currentState.Enter(); } // Update is called once per frame void Update() { if (currentState != null) currentState.UpdateLogic(); } void LateUpdate() { if (currentState != null) currentState.UpdatePhysics(); } public void ChangeState(BaseState newState) { currentState.Exit(); currentState = newState; currentState.Enter(); } protected virtual BaseState GetInitialState() { return null; } private void OnGUI() { // string content = currentState != null ? currentState.name : ("no current state!"); //GUILayout.Label($" (content) size > color >); string content = currentState.name; Debug.Log(content); if (currentState == null) { Debug.Log("no state"); } // GUILayout.Label(content); Debug.Log(content); } } (Equivalent to the tutorial's MovementSM.cs) using System.Collections; using System.Collections.Generic; using UnityEngine; public class TEBody_StateMachine: StateMachine { public Renderer ren; public Material legColor; public Material PelvisColor; [HideInInspector] public State_Move_Pelvis pelvisState; [HideInInspector] public State_MoveLegs legsState; private void Awake() { pelvisState = new State_Move_Pelvis(this); legsState = new State_MoveLegs(this); } protected override BaseState GetInitialState() { return legsState; } } State 1: Legs equivalent to the tutorial's "idle.cs" using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.InputSystem; public class State_MoveLegs : BaseState { public State_MoveLegs(TEBody_StateMachine stateMachine) : base("legs", stateMachine) { } public override void Enter() { base.Enter(); } public override void UpdateLogic() { base.UpdateLogic(); // if (GameObject.Find("Sphere").GetComponent().ren.material == GameObject.Find("Sphere").GetComponent().legColor && Input.GetKey(KeyCode.A)) if (Keyboard.current.aKey.isPressed == true) Debug.Log("A Press"); stateMachine.ChangeState(((TEBody_StateMachine) stateMachine).pelvisState); GameObject.Find("Sphere").GetComponent().ren.material = GameObject.Find("Sphere").GetComponent().PelvisColor; Debug.Log("Change to Pelvis State"); } } State 2 eqivalent to the tutorial's "Moving.cs" using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.InputSystem; public class State_Move_Pelvis : BaseState { // Start is called before the first frame update public State_Move_Pelvis(TEBody_StateMachine stateMachine) : base("pelvis", stateMachine) { } public override void Enter() { base.Enter(); } public override void UpdateLogic() { base.UpdateLogic(); //transition to another state if this input is different from pelvis... //if(GameObject.Find("Sphere").GetComponent().ren.material == GameObject.Find("Sphere").GetComponent().PelvisColor && Input.GetKey(KeyCode.A)) if (Keyboard.current.bKey.isPressed == true) Debug.Log("B Press"); stateMachine.ChangeState(((TEBody_StateMachine)stateMachine).legsState); Debug.Log("Change to Leg State"); GameObject.Find("Sphere").GetComponent().ren.material = GameObject.Find("Sphere").GetComponent().legColor; } }

Viewing latest article 22
Browse Latest Browse All 42

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>