Adding Player States
Last updated
Last updated
In the desired folder, right-click and select Create -> C# UHFPS Templates -> PlayerState Script to generate a new state script.
Open the state script in the script editor and change the value of the StateKey property to the name you want to use when switching to this state from an external script. In addition, edit the Name property to name the state within the state group.
The InitState() method is used to create a state instance and initialize the state in the State Machine script. You can also make further adjustments to the state constructor to provide the state with additional references, such as reference to a player state group or state asset to use public variables.
In my state, I have added variables to the state asset that I want to use in the player state. In order to access these variables, I added a reference to the state asset in the state constructor.
In order to create a transition to another state, you must write new transitions within the OnGetTransitions() method. In my state, I have defined that I want to switch to the Idle state when the switchToIdle variable becomes true.
The Transition.To method requires a State Key to determine which state to transition to and a Condition for when the transition should occur. The condition can be a complex lambda statement that utilizes the Input Manager or other variables to decide when to switch states.
You can also use the Transition.Back method to switch to the previous state, which can be useful in some situations.
You have the option to define multiple transitions in order to switch between various states based on specific events or conditions.
Implement logic within the OnStateEnter(), OnStateExit(), and OnStateUpdate() methods to define the actions that should take place when entering, exiting, or updating the state.
In this example, I have defined to move forward by the speed specified in the TestFloat variable. For this purpose, I used the machine.Motion variable, which is used to move the Character Controller component.
You have the option to directly modify the position by altering the Position, CenterPosition, or FeetPosition variables. Although the position might not change due to the Character Controller's influence, but you can synchronize the transform position by setting Physics.autoSyncTransforms to true or invoking the Physics.SyncTransforms() method.
Finally, add your newly created state into the States Group Asset by clicking the Add State button. The state will be assigned the name specified in the Name property.
You can examine pre-existing states from the UHFPS to get an overview of how various functionalities are implemented.