Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by MrWy07 · Apr 05 at 11:01 AM · statestate-machinestate machine

State Machine: Cannot switch states on second state enter

I have the following code in my grounded state, which works perfect on its first enter, it can be switched to any other state I want, but after entering in the grounded state for the second time, the player gets stuck in this state. Any idea why would that happen?

 using UnityEngine;
 
 public class PlayerGroundedState : PlayerBaseState
 {
     public PlayerGroundedState(PlayerStateManager currentContext, PlayerStateFactory playerStateFactory) 
     : base (currentContext, playerStateFactory) {
         IsRootState = true;
         InitializeSubstate();
     }
 
     public override void EnterState() {
         Debug.Log("grounded state; landed = " + Ctx.Landed);
         Ctx.CurrentMovementY = Ctx.GroundedGravity;
         Ctx.AppliedMovementY = Ctx.GroundedGravity;
     }
 
     public override void UpdateState() {
         CheckSwitchStates();
     }
     
     public override void ExitState() {}
     
     public override void CheckSwitchStates() {
         if(Ctx.IsJumpPressed && !Ctx.RequireNewJumpPress) {
             SwitchState(Factory.Jump());
         }
         else if(Ctx.IsFalling) {
             SwitchState(Factory.Fall());
         } 
     }
     
     public override void InitializeSubstate() {
         if(!Ctx.Landed) {
             SwitchState(Factory.Land());
         }
         else if(Ctx.IsMovementPressed) {
             SetSubState(Factory.Run());
         } 
         else {
             SetSubState(Factory.Idle());
         }
     }
 }

My abstract class

 public abstract class PlayerBaseState {
     private PlayerStateManager _ctx;
     private PlayerStateFactory _factory;
     private PlayerBaseState _currentSuperState;
     private PlayerBaseState _currentSubState;
     private bool _isRootState = false;
 
     protected bool IsRootState { set { _isRootState = value; } }
     protected PlayerStateManager Ctx { get { return _ctx; } }
     protected PlayerStateFactory Factory { get { return _factory; } } 
 
     public PlayerBaseState(PlayerStateManager currentContext, PlayerStateFactory playerStateFactory) {
         _ctx = currentContext;
         _factory = playerStateFactory;
     }
 
     public abstract void EnterState();
 
     public abstract void UpdateState();
     
     public abstract void ExitState();
     
     public abstract void CheckSwitchStates();
     
     public abstract void InitializeSubstate();
 
     public void UpdateStates() {
         UpdateState();
         if (_currentSubState != null) {
             _currentSubState.UpdateStates();
         } 
     }
 
     protected void SwitchState(PlayerBaseState newState) {
         ExitState();
         newState.EnterState();
 
         if(_isRootState) {
             _ctx.CurrentState = newState;
         } else if (_currentSuperState != null) {
             _currentSuperState.SetSubState(newState);
         }
     }
 
     protected void SetSuperState(PlayerBaseState newSuperState) {
         _currentSuperState = newSuperState;
     }
 
     protected void SetSubState(PlayerBaseState newSubState) {
         _currentSubState = newSubState;
         newSubState.SetSuperState(this);
     }
 }




Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

133 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity OnStateExit StateMachineBehaviour is never getting called 0 Answers

Interface state structure with different type of enemies 0 Answers

Easier way of creating transitions in the animator? 1 Answer

moving gameobject from state machine behaviour/different code result from inside OnStateExit() 1 Answer

How to delete a StateMachineBehaviour via Editor Script? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges