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.
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.
Implement logic within the OnStateEnter(), OnStateExit(), and OnStateUpdate() methods to define the actions that should take place when entering, exiting, or updating the state.
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.