# Joystick

## Behaviours

* Fixed (Pivot point is on touch began position and it's fixed)
* Dynamic (Pivot point is on touch began position and it's follows touch movement if it's out of radius)

## How to use

* Go to Canvas where you want to use it, usually it's GameCanvas
* Replace joystick prefab in Canvas by Create asset menu in hierarchy.\
  Right mouse click on Canvas -> Core Prefabs -> Joystick.\
  Or you can drag and drop in from \_Main\Prefabs\UI\Joystick.prefab

{% hint style="danger" %}
To avoid Joystick appear on UI elements such as buttons ->\ <mark style="color:red;">**Replace Joystick on top of other UI elements so it wont be triggered, because other elements will cover Joystick touch zone.**</mark>

Here is screen example of hierarchy with correct joystick hierarchy position:

<img src="https://1613353771-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fygjewtla1W7kzTTIpGWq%2Fuploads%2FVSONW1EV9RXWG0v685Y6%2Fimage.png?alt=media&#x26;token=fbef644f-0259-488c-ab66-4cc651d23c66" alt="" data-size="original">
{% endhint %}

* To change visuals, open Joystick.prefab and there is Stick and Background objects. You can freely change their sprites.
* Reference Joystick in script where you need it. For example in Player.cs declare variable of Joystick type.

{% hint style="warning" %} <mark style="color:red;">**To change radius and other settings of Joystick**</mark>&#x20;

-> go to **\_Main/Configs/**<mark style="color:green;">**Joystick Settings.asset**</mark>

\
\- \[<mark style="color:purple;">**Joystick radius**</mark>] is background sprite half-size, bigger radius -> player need to move touch farther to reach full movement coefficient;\
\- \[<mark style="color:purple;">**Stick sprite radius**</mark>] is stick sprite half-size, only visuals change;\
\- \[<mark style="color:purple;">**Default Joystick Behaviour**</mark>] <- what behaviour will be by default (you implement setup by SaveData.cs and JoystickBehaviours enum if you need toggle and save it yourself);\
\- \[<mark style="color:purple;">**Render Joystick On UI**</mark>] <- should be joystick sprites visible on active state?
{% endhint %}

{% hint style="info" %}
Main properties of Joystick object in script:

* MoveCoef \[0..1] returns how far player moved finger from joystick center, where center is 0.
* MoveDirection is result direction on plane XZ.
* Movement is MoveDirection \* MoveCoef
  {% endhint %}

{% hint style="success" %}
You can toggle joystick enabled state by **IsEnable** property. Useful when you need to lock player input for a while.
{% endhint %}

{% hint style="success" %}
You can switch behaviours (Fixed-Dynamic) runtime using **SwitchBehaviour()** from Joystick object.
{% endhint %}

Example of usage in **\_Examples/PlayerInput/Joystick/Joystick.scene**
