> For the complete documentation index, see [llms.txt](https://docs.twgamesdev.com/uhfps/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.twgamesdev.com/uhfps/guides/state-machines/adding-player-states.md).

# 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="/files/jev81AUN4BMZy2OYLVDk" 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="/files/ECimstzotR44fr9iOs2Q" 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="/files/70yruG9VVcNlmjZuV9V6" 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="/files/IbrxojVF1mPNnkwDlEPh" 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="/files/qPEZKjkUM1JcXHYysfuJ" 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="/files/JEHvpIJGmM7Dexs0sj3D" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/lPS2HbFUQS0o62klg8tT" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/RABsrCarwYe2iANmP9O6" 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 %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.twgamesdev.com/uhfps/guides/state-machines/adding-player-states.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
