Skip to content

herbou/Unity_EasyJoystick

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

35 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Unity Joystick input

video thumbnail


Documentation :

■ How to use ?

  1. Add EasyJoystick package to your project.
  2. Create a UI Canvas.
  3. Add the Joystick prefab (located at EasyJoystick/Prefabs) to the Canvas.

■ How to control player with the Joystick ?

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 ;
   }

}

■ Scripting :

As we've seen above, we must add EasyJoystick namespace first :

using EasyJoystick ;

Then add a reference to the Joystick :

public Joystick joystick ;

1. Properties :

Property Description
IsTouching bool: Returns whether the joystick is held or not
ArrowKeysSimulationEnabled bool: Set it to true if you want to simulate joystick with arrow keys.
you can set it through the inspector too (Joystick gameobject) :

Example :

// Use arrow keys too :
void Start (){
   joystick.ArrowKeysSimulationEnabled = true;
}

// IsTouching :
void Update (){
   if (joystick.IsTouching){
      // ...
   }
}

2. Methods :

Method Description
Horizontal() float: Returns horizontal movement between -1.0 and 1.0
Vertical() float: Returns vertical movement between -1.0 and 1.0

2. Events :

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.
   };
}




❤️ Donate

Paypal

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

Packages

No packages published

Languages