# Adding Player States

1. In the desired folder, right-click and select **Create -> C# UHFPS Templates -> PlayerState Script** to generate a new state script.

<figure><img src="https://2665493337-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fh8bp6Jx5qkS4c0NEaWR1%2Fuploads%2F1BkBNsSNBUbuocrOqako%2FScreenshot_6.png?alt=media&#x26;token=a495ba7e-aa69-4fe5-a74d-a318ece77667" alt=""><figcaption></figcaption></figure>

2. 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.

<div align="center"><figure><img src="https://2665493337-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fh8bp6Jx5qkS4c0NEaWR1%2Fuploads%2FfxAVY4fUgSvUUcJGsznR%2FScreenshot_1.png?alt=media&#x26;token=40f70e3f-e74e-4789-8239-7601efbb5a5e" alt=""><figcaption></figcaption></figure></div>

{% hint style="info" %}
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.
{% endhint %}

* 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.

<figure><img src="https://2665493337-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fh8bp6Jx5qkS4c0NEaWR1%2Fuploads%2FmGc8EfhAxch5PJPERymf%2FScreenshot_2.png?alt=media&#x26;token=aae25ff7-d382-488d-8f3c-8b324a5f7b1e" alt=""><figcaption></figcaption></figure>

3. 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.<br>

<figure><img src="https://2665493337-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fh8bp6Jx5qkS4c0NEaWR1%2Fuploads%2FkR1MrwDiWIW9vbYhlFgi%2FScreenshot_2.png?alt=media&#x26;token=b092419f-1e5c-473c-b350-0fdcf5528569" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
The **Transition.To** method requires a <mark style="color:red;">**State Key**</mark> to determine which state to transition to and a <mark style="color:red;">**Condition**</mark> 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.
{% endhint %}

{% hint style="info" %}
You can also use the **Transition.Back** method to switch to the previous state, which can be useful in some situations.
{% endhint %}

{% hint style="info" %}
You have the option to define multiple transitions in order to switch between various states based on specific events or conditions.
{% endhint %}

4. Implement logic within the **OnStateEnter()**, **OnStateExit()**, and **OnStateUpdate()** methods to define the actions that should take place when entering, exiting, or updating the state.

<figure><img src="https://2665493337-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fh8bp6Jx5qkS4c0NEaWR1%2Fuploads%2FAfSEiFV1IlQ0sfBjTvYq%2FScreenshot_7.png?alt=media&#x26;token=682b82e6-ee71-4232-9ee7-d9e1790282e1" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
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.
{% endhint %}

{% hint style="warning" %}
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**](https://docs.unity3d.com/ScriptReference/Physics-autoSyncTransforms.html) to true or invoking the [**Physics.SyncTransforms()**](https://docs.unity3d.com/ScriptReference/Physics.SyncTransforms.html) method.
{% endhint %}

5. 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.

<figure><img src="https://2665493337-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fh8bp6Jx5qkS4c0NEaWR1%2Fuploads%2FufsRd8MLxmY2Lq22W632%2FScreenshot_4.png?alt=media&#x26;token=55431a16-bcc7-4f01-828d-c996f417ed9b" alt=""><figcaption></figcaption></figure>

<figure><img src="https://2665493337-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fh8bp6Jx5qkS4c0NEaWR1%2Fuploads%2FLLtmWnc6csIFBYwddNvX%2FScreenshot_3.png?alt=media&#x26;token=a50c9ec3-85b6-4fb3-ad64-2d08f6860f60" alt=""><figcaption></figcaption></figure>

<figure><img src="https://2665493337-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fh8bp6Jx5qkS4c0NEaWR1%2Fuploads%2FkPDgRGjklr6PVAZLMVRW%2FScreenshot_4.png?alt=media&#x26;token=04ee1f34-82d9-4580-8c8c-2dabed4667d8" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
You can examine pre-existing states from the **UHFPS** to get an overview of how various functionalities are implemented.
{% endhint %}
