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. Dismiss Notice

Unity 4.6 Mobile Joystick

Discussion in 'UGUI & TextMesh Pro' started by MR_Ford, Oct 31, 2014.

  1. MR_Ford

    MR_Ford

    Joined:
    Dec 27, 2013
    Posts:
    5
    I updated the Joystick script that uses Notification Center for use with the new GUI setup in 4.6 beta.

    Set up switch out the old joystick scripts and add this one, you can leave the one attached to the player alone. As, this will still output the same for position. You will have to add a background button image which doubles as the size and hit zone for the touch pad option.

    Code (JavaScript):
    1. //////////////////////////////////////////////////////////////
    2. // Joystick.js
    3. // Penelope iPhone Tutorial
    4. // Moded by Hillary J. Ford for use with the new GUI system
    5. // in Unity 4.6
    6. // Joystick creates a movable joystick (via RectTransform) that
    7. // handles touch input, taps, and phases. Dead zones can control
    8. // where the joystick input gets picked up and can be normalized.
    9. //
    10. // Optionally, you can enable the touchPad property from the editor
    11. // to treat this Joystick as a TouchPad. A TouchPad allows the finger
    12. // to touch down at any point and it tracks the movement relatively
    13. // without moving the graphic
    14. //////////////////////////////////////////////////////////////
    15.  
    16. #pragma strict
    17.  
    18. import UnityEngine.UI;
    19.  
    20. class Boundary
    21. {
    22.     var min : Vector2 = Vector2.zero;
    23.     var max : Vector2 = Vector2.zero;
    24. }
    25. static private var joysticks : Joystick_mod[];                    // A static collection of all joysticks
    26. static private var enumeratedJoysticks : boolean = false;
    27. static private var tapTimeDelta : float = 0.3;                // Time allowed between taps
    28.  
    29. var touchPad : boolean;                                     // Is this a TouchPad?
    30. var touchZone : Rect;
    31. var position : Vector2;                                     // [-1, 1] in x,y
    32. var tapCount : int;                                            // Current tap count
    33.  
    34. private var lastFingerId = -1;                                // Finger last used for this joystick
    35. private var tapTimeWindow : float;                            // How much time there is left for a tap to occur
    36. private var fingerDownPos : Vector2;
    37. private var fingerDownTime : float;
    38. private var firstDeltaTime : float = 0.5;
    39.  
    40. var button : RectTransform;
    41. var ButtonBG_TouchPad  : RectTransform;                                    // Joystick graphic
    42. private var defaultRect : Rect;                                // Default position / extents of the joystick graphic
    43.  
    44. var isPaused:boolean = false;
    45.  
    46. function Start()
    47. {
    48.     // Store the default rect , so we can snap back to it
    49.     defaultRect.x = ButtonBG_TouchPad.rect.center.x+ButtonBG_TouchPad.position.x;
    50.     defaultRect.y = ButtonBG_TouchPad.rect.center.y+ButtonBG_TouchPad.position.y;
    51.    
    52.     if ( touchPad )
    53.     {
    54.         // If a texture has been assigned, then use the rect from the gui as our touchZone
    55.         if ( ButtonBG_TouchPad )
    56.             //touchZone = ButtonBG_TouchPad.rect;
    57.             touchZone.width = ButtonBG_TouchPad.rect.width;
    58.             touchZone.height = ButtonBG_TouchPad.rect.height;
    59.             touchZone.x = ButtonBG_TouchPad.position.x;
    60.             touchZone.y = ButtonBG_TouchPad.position.y;
    61.     }
    62.  
    63.     NotificationCenter.DefaultCenter().AddObserver(this, "Pause");
    64.     NotificationCenter.DefaultCenter().AddObserver(this, "Unpause");
    65.     NotificationCenter.DefaultCenter().AddObserver(this, "GameOver");
    66.     NotificationCenter.DefaultCenter().AddObserver(this, "WonRound");
    67. }
    68.  
    69.  
    70. function Disable()
    71. {
    72.     gameObject.SetActive(false);
    73.     enumeratedJoysticks = false;
    74. }
    75.  
    76. function Pause()
    77. {
    78.     ResetJoystick();
    79.     isPaused = true;
    80. }
    81.  
    82. function Unpause()
    83. {
    84.     ResetJoystick();
    85.     isPaused = false;
    86. }
    87.  
    88. function GameOver() {
    89.     Pause();
    90. }
    91.  
    92. function WonRound() {
    93.     Pause();
    94. }
    95.  
    96.  
    97. function ResetJoystick()
    98. {
    99.     // Release the finger control and set the joystick back to the default position
    100.     button.position.x = ButtonBG_TouchPad.rect.center.x+ButtonBG_TouchPad.position.x;
    101.     button.position.y = ButtonBG_TouchPad.rect.center.y+ButtonBG_TouchPad.position.y;
    102.     lastFingerId = -1;
    103.     position = Vector2.zero;
    104.     fingerDownPos = Vector2.zero;
    105. }
    106.  
    107. function IsFingerDown() : boolean
    108. {
    109.     return (lastFingerId != -1);
    110. }
    111.  
    112. function LatchedFinger( fingerId : int )
    113. {
    114.     // If another joystick has latched this finger, then we must release it
    115.     if ( lastFingerId == fingerId )
    116.         ResetJoystick();
    117. }
    118.  
    119. function Update()
    120. {
    121.     if (!isPaused)
    122.     {  
    123.         if ( !enumeratedJoysticks )
    124.         {
    125.             // Collect all joysticks in the game, so we can relay finger latching messages
    126.             joysticks = FindObjectsOfType( Joystick_mod ) as Joystick_mod[];
    127.             enumeratedJoysticks = true;
    128.         }  
    129.          
    130.         var count = Input.touchCount;
    131.      
    132.         // Adjust the tap time window while it still available
    133.         if ( tapTimeWindow > 0 )
    134.             tapTimeWindow -= Time.deltaTime;
    135.         else
    136.             tapCount = 0;
    137.      
    138.         if ( count == 0 )
    139.             ResetJoystick();
    140.         else
    141.         {
    142.             for(var i = 0;i < count; i++)
    143.             {
    144.                 var touch : Touch = Input.GetTouch(i);          
    145.      
    146.                 var shouldLatchFinger = false;
    147.                 if ( touchPad )
    148.                 {              
    149.                     if ( touchZone.Contains( touch.position ) )
    150.                         shouldLatchFinger = true;
    151.                 }
    152.                 else if ( hitme( touch ) )
    153.                 {
    154.                     shouldLatchFinger = true;
    155.                 }      
    156.      
    157.                 // Latch the finger if this is a new touch
    158.                 if ( shouldLatchFinger && ( lastFingerId == -1 || lastFingerId != touch.fingerId ) )
    159.                 {
    160.                  
    161.                     if ( touchPad )
    162.                     {                      
    163.                         lastFingerId = touch.fingerId;
    164.                         fingerDownPos = touch.position;
    165.                         fingerDownTime = Time.time;
    166.                     }
    167.                  
    168.                     lastFingerId = touch.fingerId;
    169.                  
    170.                     // Accumulate taps if it is within the time window
    171.                     if ( tapTimeWindow > 0 )
    172.                         tapCount++;
    173.                     else
    174.                     {
    175.                         tapCount = 1;
    176.                         tapTimeWindow = tapTimeDelta;
    177.                     }
    178.                                              
    179.                     // Tell other joysticks we've latched this finger
    180.                     for ( var j : Joystick_mod in joysticks )
    181.                     {
    182.                         if ( j != this )
    183.                             j.LatchedFinger( touch.fingerId );
    184.                     }                      
    185.                 }              
    186.      
    187.                 if ( lastFingerId == touch.fingerId )
    188.                 {  
    189.                     // Override the tap count with what the iPhone SDK reports if it is greater
    190.                     // This is a workaround, since the iPhone SDK does not currently track taps
    191.                     // for multiple touches
    192.                     if ( touch.tapCount > tapCount )
    193.                         tapCount = touch.tapCount;
    194.                  
    195.                     if ( touchPad )
    196.                     {  
    197.                         // For a touchpad, let's just set the position directly based on distance from initial touchdown
    198.                         position.x = Mathf.Clamp( ( touch.position.x - fingerDownPos.x ) / ( touchZone.width / 2 ), -1, 1 );
    199.                         position.y = Mathf.Clamp( ( touch.position.y - fingerDownPos.y ) / ( touchZone.height / 2 ), -1, 1 );
    200.                     }
    201.                     else
    202.                     {                  
    203.                         //Do something with the touches
    204.                         button.position.x = Mathf.Clamp(touch.position.x, ButtonBG_TouchPad.position.x+(button.rect.width/2), ButtonBG_TouchPad.position.x+ButtonBG_TouchPad.rect.width-(button.rect.width/2));
    205.                         button.position.y = Mathf.Clamp(touch.position.y, ButtonBG_TouchPad.position.y+(button.rect.height/2), ButtonBG_TouchPad.position.y+ButtonBG_TouchPad.rect.height-(button.rect.height/2));
    206.  
    207.                         // Get a value between -1 and 1 based on the joystick graphic location
    208.                         if (button.position.x < defaultRect.x)
    209.                                 position.x = -1;
    210.                         else
    211.                         if(button.position.x > defaultRect.x)
    212.                                 position.x = 1;
    213.                         else
    214.                         if (button.position.x == defaultRect.x)
    215.                                 position.x = 0;
    216.                              
    217.                         if (button.position.y < defaultRect.y)
    218.                                 position.y = -1;
    219.                         else
    220.                         if(button.position.y > defaultRect.y)
    221.                                 position.y = 1;
    222.                         else
    223.                         if (button.position.y == defaultRect.y)
    224.                                 position.y = 0;                      
    225.                          
    226.                     }
    227.                  
    228.                     if ( touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled ) {
    229.                         ResetJoystick();
    230.                     }
    231.                 }          
    232.             }
    233.         }
    234.     }
    235. }
    236.  
    237.  
    238. function hitme(hit:Touch) : boolean
    239. {
    240.     if( hit.position.x>=ButtonBG_TouchPad.position.x && hit.position.x <= ButtonBG_TouchPad.position.x+ButtonBG_TouchPad.rect.width)
    241.     {
    242.         if(hit.position.y>=ButtonBG_TouchPad.position.y && hit.position.y <= ButtonBG_TouchPad.position.y+ButtonBG_TouchPad.rect.height)
    243.         {
    244.             return true;
    245.         }else{ return false;}
    246.     }else{ return false;}
    247. }
     

    Attached Files:

  2. Gekigengar

    Gekigengar

    Joined:
    Jan 20, 2013
    Posts:
    705
    Why not C# If its for mobile?
     
  3. MR_Ford

    MR_Ford

    Joined:
    Dec 27, 2013
    Posts:
    5
    I am not that good programming in c# also the script was already in JS which is why I used it.

    Is there a difference between C# and JS when it comes to mobile?
     
  4. JoeVoxel

    JoeVoxel

    Joined:
    Sep 28, 2012
    Posts:
    127

    Hello MR_Ford,

    thank you very much for this script conversion.
    The touch_pad work well but I've a problem with the normal joystick mode.
    When I touch the screen the joystick button flies away out of the screen on my ipad.

    - I've replaced the old script Joystick by yours
    - created a panel for the image background
    - assign this panel in the joystick script slot.
    - launch the scene on ipad
    - when I touch the joystick, it moves away from screen.

    I don't know how to fix this.

    Can you help me please ?