Video tutorial: https://youtu.be/J3lZr6NRkiA
- Add
EasyJoystick
package to your project. - Create a UI Canvas.
- Add the
Joystick
prefab (located atEasyJoystick/Prefabs
) to the Canvas.
Default player movement script using keyboard arrow keys :
using UnityEngine ;
public class Player : MonoBehaviour {
[SerializeField] private float speed ;
private void Update () {
float xMovement = Input.GetAxis ("Horizontal") ;
float zMovement = Input.GetAxis ("Vertical") ;
transform.position += new Vector3 (xMovement, 0f, zMovement) * speed * Time.deltaTime ;
}
}
The player movement using our Joystick 😊 :
using UnityEngine ;
using EasyJoystick ; //line added
public class Player : MonoBehaviour {
[SerializeField] private float speed ;
[SerializeField] private Joystick joystick ; //line added
private void Update () {
float xMovement = joystick.Horizontal () ; //line changed
float zMovement = joystick.Vertical () ; //line changed
transform.position += new Vector3 (xMovement, 0f, zMovement) * speed * Time.deltaTime ;
}
}
As we've seen above, we must add EasyJoystick
namespace first :
using EasyJoystick ;
Then add a reference to the Joystick :
public Joystick joystick ;
Example :
// Use arrow keys too :
void Start (){
joystick.ArrowKeysSimulationEnabled = true;
}
// IsTouching :
void Update (){
if (joystick.IsTouching){
// ...
}
}
Method | Description |
---|---|
Horizontal() | float : Returns horizontal movement between -1.0 and 1.0 |
Vertical() | float : Returns vertical movement between -1.0 and 1.0 |
Event | Description |
---|---|
OnJoystickDownAction | Executes once when the joystick is touched |
OnJoystickUpAction | Executes once when you lift your finger. |
Example :
void Start (){
joystick.OnJoystickUpAction += ()=>{
// this block of code always executes once when you lift your finger.
};
}