Mobile_Joystick_Controller Using C# Scripts In Unity

Introduction

 
This article is about using  Mobile_ Joystick_Controller in Unity.
 
Prerequisites
 
Unity Environment version 2018.4.19f1
 

Create the project

 
 Mobile_Joystick_Controller Using C# Scripts In Unity
 
First, you have to open the Unity project. Create a plane in your project.
 
 Mobile_Joystick_Controller Using C# Scripts In Unity
 
Create a sphere in your project.
 
Click on the "GameObject" menu in the menu bar. Select Create 3D object (Sphere). The Create Sphere will be added to the scene View. 
 
 Mobile_Joystick_Controller Using C# Scripts In Unity
 
Create the Canvas
 
Click on the "GameObject" menu in the menu bar. Select Create UI (Canvas). The Create canvas will be added to the scene View.
 
 Mobile_Joystick_Controller Using C# Scripts In Unity
 
Click on the Asset Store. Click on the "Import Joystick Pack".
 
 Mobile_Joystick_Controller Using C# Scripts In Unity
 
Drag and drop the Fixed Joystick scripts to Canvas in this scene view. 
 
 Mobile_Joystick_Controller Using C# Scripts In Unity
 
Click on the "GameObject" menu in the menu bar. Select Create UI (Image). The Create Image will be added to the scene View.
 
 Mobile_Joystick_Controller Using C# Scripts In Unity
 
Rename the Image as fixed joystick
 
 Mobile_Joystick_Controller Using C# Scripts In Unity
 

Create C# Script

 
Right click on Assets. Select Create >> C# script
 
 Mobile_Joystick_Controller Using C# Scripts In Unity
 
Rename the script as Joybutton & My scripts.
 
 Mobile_Joystick_Controller Using C# Scripts In Unity
 
Write the coding like the following,
 
Double click on the Joybutton. The MonoDevelop-Unity editor will open up.
  1. using UnityEngine;  
  2. using UnityEngine.EventSystems;  
  3. public class Joybutton: MonoBehaviour, IPointerUpHandler, IPointerDownHandler {  
  4.     [HideInInspector]  
  5.     public bool pressed;  
  6.     // Start is called before the first frame update  
  7.     void Start() {}  
  8.     // Update is called once per frame  
  9.     void Update() {}  
  10.     public void OnPointerDown(PointerEventData eventData) {  
  11.         pressed = true;  
  12.     }  
  13.     public void OnPointerUp(PointerEventData eventData) {  
  14.         pressed = false;  
  15.     }  
  16. }   
Save the program.
 
Drag and drop the Joybutton scripts to Fixed Joystick in Mobile_Joystick_Controller scene view.
 
 Mobile_Joystick_Controller Using C# Scripts In Unity
 
Write the coding like the following,
 
Double click on the Myscripts . The MonoDevelop-Unity editor will open up
  1. using System.Collections;  
  2. using System.Collections.Generic;  
  3. using UnityEngine;  
  4. public class MyScripts: MonoBehaviour {  
  5.     protected Joystick joystick;  
  6.     protected Joybutton joybutton;  
  7.     protected bool jump;  
  8.     //public float Horizontal { get; private set; }  
  9.     // Start is called before the first frame update  
  10.     void Start() {  
  11.         joystick = FindObjectOfType < Joystick > ();  
  12.         joybutton = FindObjectOfType < Joybutton > ();  
  13.     }  
  14.     // Update is called once per frame  
  15.     void Update() {  
  16.         var rigidbody = GetComponent < Rigidbody > ();  
  17.         rigidbody.velocity = new Vector3(joystick.Horizontal * 10 f + (Input.GetAxis("Horizontal") * 10 f), rigidbody.velocity.y, joystick.Vertical * 10 f + Input.GetAxis("Vertical") * 10 f);  
  18.         if (!jump && (joybutton.pressed || Input.GetButton("Fire2"))) {  
  19.             jump = true;  
  20.             rigidbody.velocity += Vector3.up * 10 f;  
  21.         }  
  22.         if (jump && !joybutton.pressed || Input.GetButton("Fire2")) {  
  23.             jump = false;  
  24.         }  
  25.     }  
  26. }   
Save the program.
 
Go back to Unity. Click on the Myscripts script.Drag and drop the Myscripts script to Sphere in Mobile_Joystick_Controller scene view.
 
 Mobile_Joystick_Controller Using C# Scripts In Unity
 
Add the rigidbody onto the Sphere
 
 Mobile_Joystick_Controller Using C# Scripts In Unity
 
Run the app, and you will able to see a floating Mobile_Joystick_Controller.
 
 Mobile_Joystick_Controller Using C# Scripts In Unity
 

Summary

 
I hope you understood how to input through Mobile_Joystick_Controller in Unity.


Similar Articles