Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

[SOLVED]Joystick Questions[WITH CODE]

Discussion in 'Scripting' started by BrowDaddy1, Aug 14, 2016.

  1. BrowDaddy1

    BrowDaddy1

    Joined:
    May 20, 2015
    Posts:
    6
    I am trying to make a simple mobile game where half of the screen (held in landscape) is a joystick and the other half is used for an attack. How would I attempt this?

    Thank you :)
     
  2. DroidifyDevs

    DroidifyDevs

    Joined:
    Jun 24, 2015
    Posts:
    1,724
    Get the Standard Assets package. It comes with a joystick prefab.
     
  3. BrowDaddy1

    BrowDaddy1

    Joined:
    May 20, 2015
    Posts:
    6
    I actually solved my issue with a different asset and the code is:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.EventSystems;
    3. using UnityEngine.UI;
    4.  
    5. public class Joystick : MonoBehaviour , IPointerUpHandler , IDragHandler {
    6. #region Variables
    7.     [Tooltip("Make X = Y")]
    8.     public Vector2 TouchAria = new Vector2 (300, 300);
    9.     private Vector3 startPos;
    10.     [Tooltip("Call to get output on other code.")]
    11.     public Vector2 JoystickInput;
    12.     [HideInInspector] public Vector3 last; // vector to set the Gizmo
    13.     [Tooltip("Hide Joystick if not in use.")]
    14.     public bool hideJoystick = false;
    15.     private Image JoystickImage; //If you want the object to hide it will try to find the image on the object
    16.     private bool startAction; // is called when you want the object to start on drag
    17.     [Tooltip("Start joystick at 0 in any aria.")]
    18.     public bool startActionAtDrag;
    19.     [Tooltip("Turn On To Use Touch From Remote.")]
    20.     public bool UsingUnityRemote;
    21.     private Touch TouchOnJoystick;
    22.     private int ID;
    23.  
    24.     public bool stillMove;
    25.  
    26.     /// <summary>
    27.     /// If using mouse input.
    28.     /// </summary>
    29.     private Touch FakeTouch = new Touch();
    30.     private bool FakeID;
    31. #endregion
    32.     void OnEnable ()
    33.     {
    34.  
    35.         startPos = transform.position;
    36.         if (gameObject.GetComponent<Image> ()) {
    37.             JoystickImage = gameObject.GetComponent<Image> ();  
    38.         }
    39.         if (JoystickImage && hideJoystick == true) {
    40.             JoystickImage.CrossFadeAlpha (0, 0, true);
    41.         }
    42.     }
    43.  
    44.     void Update()
    45.     {
    46.         if(JoystickInput.x > 1 || JoystickInput.y > 1 || JoystickInput.x < -1 || JoystickInput.y < -1)
    47.         {
    48.             stillMove = false;
    49.         } else
    50.         {
    51.             stillMove = true;
    52.         }
    53.     }
    54.  
    55.     void LateUpdate(){
    56.         /*This assures that each object is assigned it's own Touch ID so that you do not activate other buttons on drag*/
    57. #if !UNITY_EDITOR
    58.         if (hideJoystick) {
    59.             if(Input.touchCount <= 0){
    60.                 ID = -1;
    61.             }
    62.             else if(ID == -1 && Input.touchCount>0){
    63.                 for (var i = 0; i < Input.touchCount; ++i) {
    64.                     if (Mathf.Abs(Input.touches [i].position.x - transform.position.x) < TouchAria.x/2 &&
    65.                         Mathf.Abs(Input.touches [i].position.y- transform.position.y) < TouchAria.y/2 ){
    66.                     if(Input.touches [i].phase == TouchPhase.Began){
    67.                         ID = Input.touches [i].fingerId;
    68.                     }
    69.                     }
    70.                 }
    71.             }
    72.             for (var i = 0; i < Input.touchCount; ++i) {
    73.                 if(ID == Input.touches [i].fingerId){
    74.  
    75.                     FollowFinger (Input.touches [i]);
    76.                 }
    77.             }
    78.         }
    79. #else
    80.         if(!UsingUnityRemote){
    81.         if(Input.GetMouseButtonUp(0)){
    82.             FakeID = false;
    83.             startAction = false;
    84.             startPos = transform.position;
    85.             transform.position = last;
    86.             UpdateVirtualAxes (startPos);
    87.             if (JoystickImage && hideJoystick == true) {
    88.                 JoystickImage.CrossFadeAlpha (0, .2f, true);
    89.             }
    90.         }
    91.         else if(FakeID == false && Input.GetMouseButtonDown(0)){
    92.             if (Mathf.Abs(Input.mousePosition.x - transform.position.x) < TouchAria.x/2 &&
    93.                 Mathf.Abs(Input.mousePosition.y- transform.position.y) < TouchAria.y/2){
    94.                 FakeID = true;
    95.             }
    96.                 }
    97.  
    98.         if(FakeID == true){
    99.             FollowFinger (FakeTouch);
    100.         }
    101.  
    102.         }
    103.         else{
    104.             if (hideJoystick) {
    105.                 if(Input.touchCount <= 0){
    106.                     ID = -1;
    107.                 }
    108.                 else if(ID == -1 && Input.touchCount>0){
    109.                     for (var i = 0; i < Input.touchCount; ++i) {
    110.                         if (Mathf.Abs(Input.touches [i].position.x - transform.position.x) < TouchAria.x/2 &&
    111.                             Mathf.Abs(Input.touches [i].position.y- transform.position.y) < TouchAria.y/2 ){
    112.                             if(Input.touches [i].phase == TouchPhase.Began){
    113.                                 ID = Input.touches [i].fingerId;
    114.                             }
    115.                         }
    116.                     }
    117.                 }
    118.                 for (var i = 0; i < Input.touchCount; ++i) {
    119.                     if(ID == Input.touches [i].fingerId){
    120.                        
    121.                         FollowFinger (Input.touches [i]);
    122.                     }
    123.                 }
    124.             }
    125.         }
    126. #endif
    127.  
    128.     }
    129.  
    130.     private void UpdateVirtualAxes (Vector3 value) {
    131.  
    132.         var delta = startPos - value;
    133.  
    134.         delta.x /= TouchAria.x;
    135.         delta.y /= TouchAria.y;
    136.        
    137.         JoystickInput = new Vector2 (-delta.x, -delta.y) * 2;
    138.     }
    139.  
    140.     public void FollowFinger(Touch _Touch){
    141.         Vector3 data;
    142. #if !UNITY_EDITOR
    143.  
    144.         TouchOnJoystick =  _Touch;
    145.         if (Mathf.Abs(TouchOnJoystick.position.x - transform.position.x) < TouchAria.x/2 &&                                    //
    146.             Mathf.Abs(TouchOnJoystick.position.y- transform.position.y) < TouchAria.y/2 ){                                     //
    147.  
    148.             gameObject.transform.position = new Vector3( _Touch.position.x, _Touch.position.y,transform.position.z);
    149.                 if (JoystickImage) {
    150.                     JoystickImage.CrossFadeAlpha (1, .2f, true);
    151.                 }
    152.                 startAction = true;
    153.             }
    154.             if(startActionAtDrag && TouchOnJoystick.phase == TouchPhase.Began){
    155.                 startPos = transform.position;
    156.             }
    157.             if (startAction) {
    158.  
    159.                 data = new Vector3(TouchOnJoystick.position.x,TouchOnJoystick.position.y,transform.position.z);
    160.                 Vector3 newPos = Vector3.zero;
    161.                
    162.                
    163.                 float deltax = (int)(data.x - startPos.x);
    164.                 //deltax = Mathf.Clamp(deltax, -TouchAria.x / 2, TouchAria.x / 2);
    165.                 newPos.x = deltax;
    166.  
    167.  
    168.  
    169.                 float deltay = (int)(data.y - startPos.y);
    170.                 //deltay = Mathf.Clamp(deltay, -TouchAria.y / 2, TouchAria.y / 2);
    171.                 newPos.y = deltay;
    172.  
    173.                 transform.position = Vector3.ClampMagnitude(new Vector3(newPos.x, newPos.y, newPos.z), TouchAria.x / 2) + startPos;
    174.  
    175.                 //transform.position = new Vector3(startPos.x + newPos.x, startPos.y + newPos.y, startPos.z + newPos.z);
    176.                 UpdateVirtualAxes (transform.position);
    177.             }
    178.            
    179.  
    180.  
    181.  
    182.        
    183.         if (TouchOnJoystick.phase == TouchPhase.Ended) {
    184.  
    185.             startAction = false;
    186.             if (JoystickImage) {
    187.                 JoystickImage.CrossFadeAlpha (0, .1f, true);
    188.             }
    189.             transform.position = last;
    190.             UpdateVirtualAxes (startPos);
    191.         }
    192.  
    193. #else
    194.         if (!UsingUnityRemote){
    195. if (Mathf.Abs(Input.mousePosition.x - transform.position.x) < TouchAria.x/2 &&                   //
    196.             Mathf.Abs(Input.mousePosition.y- transform.position.y) < TouchAria.y/2  && Input.GetMouseButton(0)){      //
    197.             gameObject.transform.position = new Vector3(Input.mousePosition.x,Input.mousePosition.y,transform.position.z);
    198.             if (JoystickImage) {
    199.                 JoystickImage.CrossFadeAlpha (1, .2f, true);
    200.             }
    201.             startAction = true;
    202.         }
    203.  
    204.         if(startActionAtDrag && Input.GetMouseButtonDown(0)){
    205.             startPos = transform.position;
    206.         }
    207.  
    208.    
    209.     if (startAction) {
    210.             data = new Vector3(Input.mousePosition.x,Input.mousePosition.y,transform.position.z);
    211.                         Vector3 newPos = Vector3.zero;
    212.  
    213.  
    214.                 float deltax = (int)(data.x - startPos.x);
    215.                 //deltax = Mathf.Clamp(deltax, -TouchAria.x / 2, TouchAria.x / 2);
    216.                 newPos.x = deltax;
    217.  
    218.  
    219.  
    220.                 float deltay = (int)(data.y - startPos.y);
    221.                 //deltay = Mathf.Clamp(deltay, -TouchAria.y / 2, TouchAria.y / 2);
    222.                 newPos.y = deltay;
    223.  
    224.                 transform.position = Vector3.ClampMagnitude(new Vector3(newPos.x, newPos.y, newPos.z), TouchAria.x / 2) + startPos;
    225.  
    226.                 //transform.position = new Vector3(startPos.x + newPos.x, startPos.y + newPos.y, startPos.z + newPos.z);
    227.  
    228.                 //transform.position = new Vector3(startPos.x + newPos.x, startPos.y + newPos.y, startPos.z + newPos.z);
    229.                 UpdateVirtualAxes(transform.position);
    230.                 }
    231.  
    232.         if (Input.GetMouseButtonUp (0)) {
    233.             startAction = false;
    234.  
    235.             if (JoystickImage) {
    236.                 JoystickImage.CrossFadeAlpha (0, .2f, true);
    237.             }
    238.             transform.position = startPos;
    239.             UpdateVirtualAxes (startPos);
    240.         }
    241.         }
    242.         else{
    243.             TouchOnJoystick =  _Touch;
    244.             if (Mathf.Abs(TouchOnJoystick.position.x - transform.position.x) < TouchAria.x/2 &&             //  /2
    245.                 Mathf.Abs(TouchOnJoystick.position.y- transform.position.y) < TouchAria.y/2 ){              //
    246.                
    247.                 gameObject.transform.position = new Vector3( _Touch.position.x, _Touch.position.y,transform.position.z);
    248.                 if (JoystickImage) {
    249.                     JoystickImage.CrossFadeAlpha (1, .2f, true);
    250.                 }
    251.                 startAction = true;
    252.             }
    253.             if(startActionAtDrag && TouchOnJoystick.phase == TouchPhase.Began){
    254.                 startPos = transform.position;
    255.             }
    256.             if (startAction) {
    257.                
    258.                 data = new Vector3(TouchOnJoystick.position.x,TouchOnJoystick.position.y,transform.position.z);
    259.                 Vector3 newPos = Vector3.zero;
    260.  
    261.  
    262.                 float deltax = (int)(data.x - startPos.x);
    263.                 //deltax = Mathf.Clamp(deltax, -TouchAria.x / 2, TouchAria.x / 2);
    264.                 newPos.x = deltax;
    265.  
    266.  
    267.  
    268.                 float deltay = (int)(data.y - startPos.y);
    269.                 //deltay = Mathf.Clamp(deltay, -TouchAria.y / 2, TouchAria.y / 2);
    270.                 newPos.y = deltay;
    271.  
    272.                 transform.position = Vector3.ClampMagnitude(new Vector3(newPos.x, newPos.y, newPos.z), TouchAria.x / 2) + startPos;
    273.  
    274.                 //transform.position = new Vector3(startPos.x + newPos.x, startPos.y + newPos.y, startPos.z + newPos.z);
    275.                 UpdateVirtualAxes(transform.position);
    276.             }
    277.            
    278.            
    279.            
    280.            
    281.            
    282.             if (TouchOnJoystick.phase == TouchPhase.Ended) {
    283.                
    284.                 startAction = false;
    285.                 if (JoystickImage) {
    286.                     JoystickImage.CrossFadeAlpha (0, .1f, true);
    287.                 }
    288.                 transform.position = last;
    289.                 UpdateVirtualAxes (startPos);
    290.             }
    291.         }
    292.  
    293. #endif
    294.  
    295.  
    296.     }
    297.  
    298.  
    299.     public  void OnDrag(PointerEventData data) {
    300.         if(!hideJoystick){
    301.                 Vector3 newPos = Vector3.zero;
    302.  
    303.  
    304.             float deltax = (int)(data.position.x - startPos.x);
    305.             //deltax = Mathf.Clamp(deltax, -TouchAria.x / 2, TouchAria.x / 2);
    306.             newPos.x = deltax;
    307.  
    308.  
    309.  
    310.             float deltay = (int)(data.position.y - startPos.y);
    311.             //deltay = Mathf.Clamp(deltay, -TouchAria.y / 2, TouchAria.y / 2);
    312.             newPos.y = deltay;
    313.  
    314.             transform.position = Vector3.ClampMagnitude(new Vector3(newPos.x, newPos.y, newPos.z), TouchAria.x / 2) + startPos;
    315.  
    316.             //transform.position = new Vector3(startPos.x + newPos.x, startPos.y + newPos.y, startPos.z + newPos.z);
    317.             UpdateVirtualAxes(transform.position);
    318.            
    319.         }
    320.     }
    321.  
    322.  
    323.     public  void OnPointerUp(PointerEventData data)
    324.     {
    325.         if(!hideJoystick){
    326.                 if (JoystickImage && hideJoystick == true) {
    327.                         JoystickImage.CrossFadeAlpha (0, .2f, true);
    328.                 }
    329.                 transform.position = startPos;
    330.                 UpdateVirtualAxes (startPos);
    331.         }
    332.     }
    333.  
    334.     void OnDrawGizmos() {
    335.         /* This is all the visual stuff to help visualize the bounds for the joystick */
    336.         Vector3 lastpos;
    337.         if (!Application.isPlaying) {
    338.                         lastpos = transform.position;
    339.             last= lastpos;
    340.                 }
    341.         Gizmos.DrawWireCube (last, new Vector3 (TouchAria.x, TouchAria.y, .1f));
    342.                
    343.     }
    344.  
    345. }
    works fine
     
    DroidifyDevs likes this.
  4. DroidifyDevs

    DroidifyDevs

    Joined:
    Jun 24, 2015
    Posts:
    1,724
    Be careful with posting code from downloaded assets. I think you aren't supposed to do that, especially with the paid assets.