Adding States

To create and integrate a new state into the player movement system, follow the steps below:

  1. Create a new C# script:

    • Navigate to Scripts > State System > Child States and create a new C# script for your state named PlayerNewStateNameState.cs.

  2. Edit the new script:

    • Open the script and delete the MonoBehaviour inheritance as well as the Start() and Update() methods.

    • Make the new state class inherit from MainState.

    • Generate or write a constructor for the new state class:

public PlayerNewStateNameState(PlayerMain player, PlayerStateMachine stateMachine, PlayerMain.AnimName animEnum, PlayerData playerData) : base(player, stateMachine, animEnum, playerData)
{
}
  1. Override virtual functions:

    • Generate or write overrides for Main State's virtual functions: Enter(), Update(), FixedUpdate(), Exit(), PhysicsCheck(), SwitchStateLogic().

  2. Add custom logic:

    • Implement your custom logic using the overridden functions.

  3. Create nested class in PlayerData (recommended):

    • If you want to create variables for the PlayerData class to control and tweak the new state, add a new nested class to the PlayerData script, keeping the structure consistent with the other nested variable classes.

  4. Update PlayerMain.cs:

    • Open the PlayerMain.cs script and update the AnimName enum by adding the new state's animation bool name.

    • Declare the new state:

public MainState NewStateNameState;
  • In the Awake() method, initialize the new state:

csharpCopy codeprivate void Awake()
{
    ...
    NewStateNameState = new PlayerNewStateNameState(this, _stateMachine, AnimName.NewStateName, PlayerData);
    ...
}
  1. Set up animations:

    • Open the "Player AC" located in the animations folder.

    • Create an empty state, connect entry and exit to it, and name it as your new state.

    • Add a new bool to the parameters with the same name as your new state's AnimBool variable.

    • Add enter and exit conditions to the new state in the animator.

    • Choose the new bool you added as the condition: Enter condition occurs when the bool is true, and exit when false.

    • For fast animation changes, in the transition to exit, untick "Has Exit Time" and set the "Transition Duration" to 0 seconds.

Now you have successfully added a new state to the player movement system! Implement your custom logic within the new state and fine-tune the animations to create unique gameplay experiences.

Last updated