Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Converted Penelope's Javascript Joysticks to C# (c sharp)

Discussion in 'iOS and tvOS' started by aaronblohowiak, Aug 30, 2009.

  1. aaronblohowiak

    aaronblohowiak

    Joined:
    Jan 23, 2009
    Posts:
    63
    Oy, this is such a PITA.. if I understood the compiler chain better, perhaps I'd be able to automate the conversion (a boy can dream, can't he?)

    In the mean time, running the following search/replace in vim (what else?) in the following order got me about 85% of the way there:

    %s/var \(.*\) : \(.*\) = \(.*\);/\2 \1 = \3;/g
    %s/var \(.*\) : \(.*\);/\2 \1;/g
    %s/function \(.*\)() : \(.*\)/public \2 \1()/g
    %s/function \(.*\)/public void \1
    %s/boolean/bool

    i was too lazy to figure out how to add 'f' to the end of floats, and the above doesnt handle changing for( var blah : Blah in blah) to foreach(Blah blah in blah) though i might add more as I go along.

    more of a PITA is getting around c#'s inability to set variables on member properties ( ie: foo.color.a becomes tmpclr = foo.color; tmpclr.a= 0.2f; foo.color = tmpclr); I decided to create class-specific private member variables to avoid dynamic variable allocation (so we dont have as much garbage to collect.)

    I hereby license my changes under the WTFPL [link]http://sam.zoy.org/wtfpl/[/link]

    Anyway, here she is:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. //////////////////////////////////////////////////////////////
    5. // Joystick.js
    6. // Penelope iPhone Tutorial
    7. //
    8. // Joystick creates a movable joystick (via GUITexture) that
    9. // handles touch input, taps, and phases. Dead zones can control
    10. // where the joystick input gets picked up and can be normalized.
    11. //
    12. // Optionally, you can enable the touchPad property from the editor
    13. // to treat this Joystick as a TouchPad. A TouchPad allows the finger
    14. // to touch down at any point and it tracks the movement relatively
    15. // without moving the graphic
    16. //////////////////////////////////////////////////////////////
    17.  
    18. // A simple class for bounding how far the GUITexture will move
    19. // converted to CS by Aaron Blohowiak, Aug 2009
    20.  
    21. public class Boundary
    22. {
    23.     public Vector2 min = Vector2.zero;
    24.     public Vector2 max = Vector2.zero;
    25. }
    26.  
    27. public class Joystick : MonoBehaviour{
    28.     static private Joystick[] joysticks;                    // A static collection of all joysticks
    29.     static private bool enumeratedJoysticks=false;
    30.     static private float tapTimeDelta = 0.3f;               // Time allowed between taps
    31.  
    32.     public bool touchPad;                                   // Is this a TouchPad?
    33.     public Rect touchZone;
    34.     public Vector2 deadZone = Vector2.zero;                     // Control when position is output
    35.     public bool normalize = false;                          // Normalize output after the dead-zone?
    36.     public Vector2 position;                                    // [-1, 1] in x,y
    37.     public int tapCount;                                            // Current tap count
    38.  
    39.     private int lastFingerId = -1;                              // Finger last used for this joystick
    40.     private float tapTimeWindow;                            // How much time there is left for a tap to occur
    41.     private Vector2 fingerDownPos;
    42.     private float fingerDownTime;
    43.     private float firstDeltaTime = 0.5f;
    44.  
    45.     private GUITexture gui;                             // Joystick graphic
    46.     private Rect defaultRect;                               // Default position / extents of the joystick graphic
    47.     private Boundary guiBoundary = new Boundary();          // Boundary for joystick graphic
    48.     private Vector2 guiTouchOffset;                     // Offset to apply to touch input
    49.     private Vector2 guiCenter;                          // Center of joystick
    50.  
    51.     private Vector3 tmpv3;
    52.     private Rect tmprect;
    53.     private Color tmpclr;
    54.  
    55.     public void Start()
    56.     {
    57.         // Cache this component at startup instead of looking up every frame   
    58.         gui = (GUITexture) GetComponent( typeof(GUITexture) );
    59.        
    60.         // Store the default rect for the gui, so we can snap back to it
    61.         defaultRect = gui.pixelInset;  
    62.        
    63.         if ( touchPad )
    64.         {
    65.             // If a texture has been assigned, then use the rect ferom the gui as our touchZone
    66.             if ( gui.texture )
    67.                 touchZone = gui.pixelInset;
    68.         }
    69.         else
    70.         {              
    71.             // This is an offset for touch input to match with the top left
    72.             // corner of the GUI
    73.             guiTouchOffset.x = defaultRect.width * 0.5f;
    74.             guiTouchOffset.y = defaultRect.height * 0.5f;
    75.            
    76.             // Cache the center of the GUI, since it doesn't change
    77.             guiCenter.x = defaultRect.x + guiTouchOffset.x;
    78.             guiCenter.y = defaultRect.y + guiTouchOffset.y;
    79.            
    80.             // Let's build the GUI boundary, so we can clamp joystick movement
    81.             guiBoundary.min.x = defaultRect.x - guiTouchOffset.x;
    82.             guiBoundary.max.x = defaultRect.x + guiTouchOffset.x;
    83.             guiBoundary.min.y = defaultRect.y - guiTouchOffset.y;
    84.             guiBoundary.max.y = defaultRect.y + guiTouchOffset.y;
    85.         }
    86.     }
    87.  
    88.     public void Disable()
    89.     {
    90.         gameObject.active = false;
    91.         enumeratedJoysticks = false;
    92.     }
    93.  
    94.     public void ResetJoystick()
    95.     {
    96.         // Release the finger control and set the joystick back to the default position
    97.         gui.pixelInset = defaultRect;
    98.         lastFingerId = -1;
    99.         position = Vector2.zero;
    100.         fingerDownPos = Vector2.zero;
    101.        
    102.         if ( touchPad ){
    103.             tmpclr  = gui.color;
    104.             tmpclr.a = 0.025f;
    105.             gui.color = tmpclr;
    106.         }  
    107.     }
    108.  
    109.     public bool IsFingerDown()
    110.     {
    111.         return (lastFingerId != -1);
    112.     }
    113.        
    114.     public void LatchedFinger( int fingerId )
    115.     {
    116.         // If another joystick has latched this finger, then we must release it
    117.         if ( lastFingerId == fingerId )
    118.             ResetJoystick();
    119.     }
    120.  
    121.     public void Update()
    122.     {  
    123.         if ( !enumeratedJoysticks )
    124.         {
    125.             // Collect all joysticks in the game, so we can relay finger latching messages
    126.             joysticks = (Joystick[])  FindObjectsOfType( typeof(Joystick) );
    127.             enumeratedJoysticks = true;
    128.         }  
    129.            
    130.         int count = iPhoneInput.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(int i = 0;i < count; i++)
    143.             {
    144.                 iPhoneTouch touch = iPhoneInput.GetTouch(i);           
    145.                 Vector2 guiTouchPos = touch.position - guiTouchOffset;
    146.        
    147.                 bool shouldLatchFinger = false;
    148.                 if ( touchPad )
    149.                 {              
    150.                     if ( touchZone.Contains( touch.position ) )
    151.                         shouldLatchFinger = true;
    152.                 }
    153.                 else if ( gui.HitTest( touch.position ) )
    154.                 {
    155.                     shouldLatchFinger = true;
    156.                 }      
    157.        
    158.                 // Latch the finger if this is a new touch
    159.                 if ( shouldLatchFinger  ( lastFingerId == -1 || lastFingerId != touch.fingerId ) )
    160.                 {
    161.                    
    162.                     if ( touchPad )
    163.                     {
    164.                         tmpclr = gui.color;
    165.                         tmpclr.a = 0.15f;
    166.                         gui.color = tmpclr;
    167.  
    168.                         lastFingerId = touch.fingerId;
    169.                         fingerDownPos = touch.position;
    170.                         fingerDownTime = Time.time;
    171.                     }
    172.                    
    173.                     lastFingerId = touch.fingerId;
    174.                    
    175.                     // Accumulate taps if it is within the time window
    176.                     if ( tapTimeWindow > 0 )
    177.                         tapCount++;
    178.                     else
    179.                     {
    180.                         tapCount = 1;
    181.                         tapTimeWindow = tapTimeDelta;
    182.                     }
    183.                                                
    184.                     // Tell other joysticks we've latched this finger
    185.                     foreach ( Joystick j in joysticks )
    186.                     {
    187.                         if ( j != this )
    188.                             j.LatchedFinger( touch.fingerId );
    189.                     }                      
    190.                 }              
    191.        
    192.                 if ( lastFingerId == touch.fingerId )
    193.                 {  
    194.                     // Override the tap count with what the iPhone SDK reports if it is greater
    195.                     // This is a workaround, since the iPhone SDK does not currently track taps
    196.                     // for multiple touches
    197.                     if ( touch.tapCount > tapCount )
    198.                         tapCount = touch.tapCount;
    199.                    
    200.                     if ( touchPad )
    201.                     {  
    202.                         // For a touchpad, let's just set the position directly based on distance from initial touchdown
    203.                         position.x = Mathf.Clamp( ( touch.position.x - fingerDownPos.x ) / ( touchZone.width / 2 ), -1, 1 );
    204.                         position.y = Mathf.Clamp( ( touch.position.y - fingerDownPos.y ) / ( touchZone.height / 2 ), -1, 1 );
    205.                     }
    206.                     else
    207.                     {                  
    208.                         // Change the location of the joystick graphic to match where the touch is
    209.                         tmprect = gui.pixelInset;
    210.                         tmprect.x = Mathf.Clamp( guiTouchPos.x, guiBoundary.min.x, guiBoundary.max.x );
    211.                         tmprect.y = Mathf.Clamp( guiTouchPos.y, guiBoundary.min.y, guiBoundary.max.y );    
    212.                         gui.pixelInset = tmprect;
    213.                     }
    214.                    
    215.                     if ( touch.phase == iPhoneTouchPhase.Ended || touch.phase == iPhoneTouchPhase.Canceled )
    216.                         ResetJoystick();                   
    217.                 }          
    218.             }
    219.         }
    220.        
    221.         if ( !touchPad )
    222.         {
    223.             // Get a value between -1 and 1 based on the joystick graphic location
    224.             position.x = ( gui.pixelInset.x + guiTouchOffset.x - guiCenter.x ) / guiTouchOffset.x;
    225.             position.y = ( gui.pixelInset.y + guiTouchOffset.y - guiCenter.y ) / guiTouchOffset.y;
    226.         }
    227.        
    228.         // Adjust for dead zone
    229.         float absoluteX = Mathf.Abs( position.x );
    230.         float absoluteY = Mathf.Abs( position.y );
    231.        
    232.         if ( absoluteX < deadZone.x )
    233.         {
    234.             // Report the joystick as being at the center if it is within the dead zone
    235.             position.x = 0;
    236.         }
    237.         else if ( normalize )
    238.         {
    239.             // Rescale the output after taking the dead zone into account
    240.             position.x = Mathf.Sign( position.x ) * ( absoluteX - deadZone.x ) / ( 1 - deadZone.x );
    241.         }
    242.            
    243.         if ( absoluteY < deadZone.y )
    244.         {
    245.             // Report the joystick as being at the center if it is within the dead zone
    246.             position.y = 0;
    247.         }
    248.         else if ( normalize )
    249.         {
    250.             // Rescale the output after taking the dead zone into account
    251.             position.y = Mathf.Sign( position.y ) * ( absoluteY - deadZone.y ) / ( 1 - deadZone.y );
    252.         }
    253.     }
    254.  
    255. }
    256.  
     
  2. TriplePAF

    TriplePAF

    Joined:
    Aug 19, 2009
    Posts:
    246
    HaHa :D I will look with all my eyeballs into this great Joystick.js to C# conversion topic.


    Peter. 8)
     
  3. grobm

    grobm

    Joined:
    Aug 15, 2005
    Posts:
    217
    I am trying to use the code to create a joystick button, currently I am not able to use the GUI buttons since the Joystick overrides the update.

    Suggestions?

    Thanks in advance,
    -Mark
     
  4. aaronblohowiak

    aaronblohowiak

    Joined:
    Jan 23, 2009
    Posts:
    63
    Grobm: I'm not sure what you mean, can you provide an example?
     
  5. grobm

    grobm

    Joined:
    Aug 15, 2005
    Posts:
    217
    Found the solution to my issue, someone else was looking for the same thing yesterday.
    http://forum.unity3d.com/viewtopic.php?t=32404

    I was looking for a simple script solution that would work at the same time I was using the joystick script input on the iphone.

    AKA while moving with input from the joystick script on the iphone, I could shoot a gun. Currently if you tried it with your the joystick script and a standard GUI layer Repeat button, it will not shoot until you remove the touch event.

    function OnGui (){
    if GUI.RepeatButton... {
    ...Fire gun message
    }
    ...

    This GUI button does not fire. :)while using the joystick script.

    Reference link above works by creating the same update method as joystick, but simply determine if touch is in GUITexture coord., if it is... fire.

    Cheers,
     
  6. AnomalusUndrdog

    AnomalusUndrdog

    Joined:
    Jul 3, 2009
    Posts:
    1,554
    Thanks so much for the code conversion Aaron
     
  7. alexxxhp

    alexxxhp

    Joined:
    Dec 9, 2010
    Posts:
    8
    anyone know how to use the joysticks as plane controls for spaceship on iphone tried everything no luck.
     
  8. 999dom999

    999dom999

    Joined:
    Aug 23, 2010
    Posts:
    14
    When using the javascript joystick.js from another js script it was done like this:

    var moveTouchPad : Joystick;

    Then you added a gameObject that had the joystick script attached to it.

    What is the Csharp version of var moveTouchPad : Joystick; ?
     
  9. mrekuc

    mrekuc

    Joined:
    Apr 17, 2009
    Posts:
    116
    public Joystick moveTouchPad;
     
  10. geniuscd

    geniuscd

    Joined:
    Aug 10, 2012
    Posts:
    23
    hey guys, thank you for the conversation :)
    i'm trying to add the script to a GUI Texture, but im not getting anything...
    how this thing works?
    I really appreciate your reply !
    thank you
     
  11. kcirdnehnosaj

    kcirdnehnosaj

    Joined:
    Jul 30, 2012
    Posts:
    5
    Here is an updated version of the C# Joystick.

    Code (csharp):
    1. using UnityEngine;
    2.  
    3. using System.Collections;
    4.  
    5. //////////////////////////////////////////////////////////////
    6.  
    7. // Joystick.js
    8.  
    9. // Penelope iPhone Tutorial
    10.  
    11. //
    12.  
    13. // Joystick creates a movable joystick (via GUITexture) that
    14.  
    15. // handles touch input, taps, and phases. Dead zones can control
    16.  
    17. // where the joystick input gets picked up and can be normalized.
    18.  
    19. //
    20.  
    21. // Optionally, you can enable the touchPad property from the editor
    22.  
    23. // to treat this Joystick as a TouchPad. A TouchPad allows the finger
    24.  
    25. // to touch down at any point and it tracks the movement relatively
    26.  
    27. // without moving the graphic
    28.  
    29. //////////////////////////////////////////////////////////////
    30.  
    31.  
    32.  
    33. // A simple class for bounding how far the GUITexture will move
    34.  
    35. // converted to CS by Aaron Blohowiak, Aug 2009
    36.  
    37.  
    38.  
    39.  
    40. public class Boundary
    41.  
    42. {
    43.  
    44.     public Vector2 min = Vector2.zero;
    45.  
    46.     public Vector2 max = Vector2.zero;
    47.  
    48. }
    49.  
    50.  
    51.  
    52. public class Joystick : MonoBehaviour{
    53.  
    54.     static private Joystick[] joysticks;                    // A static collection of all joysticks
    55.  
    56.     static private bool enumeratedJoysticks=false;
    57.  
    58.     static private float tapTimeDelta = 0.3f;               // Time allowed between taps
    59.  
    60.  
    61.  
    62.     public bool touchPad;                                   // Is this a TouchPad?
    63.  
    64.     public Rect touchZone;
    65.  
    66.     public Vector2 deadZone = Vector2.zero;                     // Control when position is output
    67.  
    68.     public bool normalize = false;                          // Normalize output after the dead-zone?
    69.  
    70.     public Vector2 position;                                    // [-1, 1] in x,y
    71.  
    72.     public int tapCount;                                            // Current tap count
    73.  
    74.  
    75.  
    76.     private int lastFingerId = -1;                              // Finger last used for this joystick
    77.  
    78.     private float tapTimeWindow;                            // How much time there is left for a tap to occur
    79.  
    80.     private Vector2 fingerDownPos;
    81.  
    82.     private float fingerDownTime;
    83.  
    84.     private float firstDeltaTime = 0.5f;
    85.  
    86.  
    87.  
    88.     private GUITexture gui;                             // Joystick graphic
    89.  
    90.     private Rect defaultRect;                               // Default position / extents of the joystick graphic
    91.  
    92.     private Boundary guiBoundary = new Boundary();          // Boundary for joystick graphic
    93.  
    94.     private Vector2 guiTouchOffset;                     // Offset to apply to touch input
    95.  
    96.     private Vector2 guiCenter;                          // Center of joystick
    97.  
    98.  
    99.  
    100.     private Vector3 tmpv3;
    101.  
    102.     private Rect tmprect;
    103.  
    104.     private Color tmpclr;
    105.  
    106.  
    107.  
    108.     public void Start()
    109.  
    110.     {
    111.  
    112.         // Cache this component at startup instead of looking up every frame    
    113.  
    114.         gui = (GUITexture) GetComponent( typeof(GUITexture) );
    115.  
    116.        
    117.  
    118.         // Store the default rect for the gui, so we can snap back to it
    119.  
    120.         defaultRect = gui.pixelInset;  
    121.  
    122.        
    123.  
    124.         if ( touchPad )
    125.  
    126.         {
    127.  
    128.             // If a texture has been assigned, then use the rect ferom the gui as our touchZone
    129.  
    130.             if ( gui.texture )
    131.  
    132.                 touchZone = gui.pixelInset;
    133.  
    134.         }
    135.  
    136.         else
    137.  
    138.         {              
    139.  
    140.             // This is an offset for touch input to match with the top left
    141.  
    142.             // corner of the GUI
    143.  
    144.             guiTouchOffset.x = defaultRect.width * 0.5f;
    145.  
    146.             guiTouchOffset.y = defaultRect.height * 0.5f;
    147.  
    148.            
    149.  
    150.             // Cache the center of the GUI, since it doesn't change
    151.  
    152.             guiCenter.x = defaultRect.x + guiTouchOffset.x;
    153.  
    154.             guiCenter.y = defaultRect.y + guiTouchOffset.y;
    155.  
    156.            
    157.  
    158.             // Let's build the GUI boundary, so we can clamp joystick movement
    159.  
    160.             guiBoundary.min.x = defaultRect.x - guiTouchOffset.x;
    161.  
    162.             guiBoundary.max.x = defaultRect.x + guiTouchOffset.x;
    163.  
    164.             guiBoundary.min.y = defaultRect.y - guiTouchOffset.y;
    165.  
    166.             guiBoundary.max.y = defaultRect.y + guiTouchOffset.y;
    167.  
    168.         }
    169.  
    170.     }
    171.  
    172.  
    173.  
    174.     public void Disable()
    175.  
    176.     {
    177.  
    178.         gameObject.active = false;
    179.  
    180.         enumeratedJoysticks = false;
    181.  
    182.     }
    183.  
    184.  
    185.  
    186.     public void ResetJoystick()
    187.  
    188.     {
    189.  
    190.         // Release the finger control and set the joystick back to the default position
    191.  
    192.         gui.pixelInset = defaultRect;
    193.  
    194.         lastFingerId = -1;
    195.  
    196.         position = Vector2.zero;
    197.  
    198.         fingerDownPos = Vector2.zero;
    199.  
    200.        
    201.  
    202.         if ( touchPad ){
    203.  
    204.             tmpclr  = gui.color;
    205.  
    206.             tmpclr.a = 0.025f;
    207.  
    208.             gui.color = tmpclr;
    209.  
    210.         }  
    211.  
    212.     }
    213.  
    214.  
    215.  
    216.     public bool IsFingerDown()
    217.  
    218.     {
    219.  
    220.         return (lastFingerId != -1);
    221.  
    222.     }
    223.  
    224.        
    225.  
    226.     public void LatchedFinger( int fingerId )
    227.  
    228.     {
    229.  
    230.         // If another joystick has latched this finger, then we must release it
    231.  
    232.         if ( lastFingerId == fingerId )
    233.  
    234.             ResetJoystick();
    235.  
    236.     }
    237.  
    238.  
    239.  
    240.     public void Update()
    241.  
    242.     {  
    243.  
    244.         if ( !enumeratedJoysticks )
    245.  
    246.         {
    247.  
    248.             // Collect all joysticks in the game, so we can relay finger latching messages
    249.  
    250.             joysticks = (Joystick[])  FindObjectsOfType( typeof(Joystick) );
    251.  
    252.             enumeratedJoysticks = true;
    253.  
    254.         }  
    255.  
    256.            
    257.  
    258.         int count = Input.touchCount;
    259.  
    260.        
    261.  
    262.         // Adjust the tap time window while it still available
    263.  
    264.         if ( tapTimeWindow > 0 )
    265.  
    266.             tapTimeWindow -= Time.deltaTime;
    267.  
    268.         else
    269.  
    270.             tapCount = 0;
    271.  
    272.        
    273.  
    274.         if ( count == 0 )
    275.  
    276.             ResetJoystick();
    277.  
    278.         else
    279.  
    280.         {
    281.  
    282.             for(int i = 0;i < count; i++)
    283.  
    284.             {
    285.  
    286.                 Touch touch = Input.GetTouch(i);            
    287.                
    288.                 Vector2 guiTouchPos = touch.position - guiTouchOffset;
    289.  
    290.        
    291.  
    292.                 bool shouldLatchFinger = false;
    293.  
    294.                 if ( touchPad )
    295.  
    296.                 {              
    297.  
    298.                     if ( touchZone.Contains( touch.position ) )
    299.  
    300.                         shouldLatchFinger = true;
    301.  
    302.                 }
    303.  
    304.                 else if ( gui.HitTest( touch.position ) )
    305.  
    306.                 {
    307.  
    308.                     shouldLatchFinger = true;
    309.  
    310.                 }      
    311.  
    312.        
    313.  
    314.                 // Latch the finger if this is a new touch
    315.  
    316.                 if ( shouldLatchFinger  ( lastFingerId == -1 || lastFingerId != touch.fingerId ) )
    317.  
    318.                 {
    319.  
    320.                    
    321.  
    322.                     if ( touchPad )
    323.  
    324.                     {
    325.  
    326.                         tmpclr = gui.color;
    327.  
    328.                         tmpclr.a = 0.15f;
    329.  
    330.                         gui.color = tmpclr;
    331.  
    332.  
    333.  
    334.                         lastFingerId = touch.fingerId;
    335.  
    336.                         fingerDownPos = touch.position;
    337.  
    338.                         fingerDownTime = Time.time;
    339.  
    340.                     }
    341.  
    342.                    
    343.  
    344.                     lastFingerId = touch.fingerId;
    345.  
    346.                    
    347.  
    348.                     // Accumulate taps if it is within the time window
    349.  
    350.                     if ( tapTimeWindow > 0 )
    351.  
    352.                         tapCount++;
    353.  
    354.                     else
    355.  
    356.                     {
    357.  
    358.                         tapCount = 1;
    359.  
    360.                         tapTimeWindow = tapTimeDelta;
    361.  
    362.                     }
    363.  
    364.                                                
    365.  
    366.                     // Tell other joysticks we've latched this finger
    367.  
    368.                     foreach ( Joystick j in joysticks )
    369.  
    370.                     {
    371.  
    372.                         if ( j != this )
    373.  
    374.                             j.LatchedFinger( touch.fingerId );
    375.  
    376.                     }                      
    377.  
    378.                 }              
    379.  
    380.        
    381.  
    382.                 if ( lastFingerId == touch.fingerId )
    383.  
    384.                 {  
    385.  
    386.                     // Override the tap count with what the iPhone SDK reports if it is greater
    387.  
    388.                     // This is a workaround, since the iPhone SDK does not currently track taps
    389.  
    390.                     // for multiple touches
    391.  
    392.                     if ( touch.tapCount > tapCount )
    393.  
    394.                         tapCount = touch.tapCount;
    395.  
    396.                    
    397.  
    398.                     if ( touchPad )
    399.  
    400.                     {  
    401.  
    402.                         // For a touchpad, let's just set the position directly based on distance from initial touchdown
    403.  
    404.                         position.x = Mathf.Clamp( ( touch.position.x - fingerDownPos.x ) / ( touchZone.width / 2 ), -1, 1 );
    405.  
    406.                         position.y = Mathf.Clamp( ( touch.position.y - fingerDownPos.y ) / ( touchZone.height / 2 ), -1, 1 );
    407.  
    408.                     }
    409.  
    410.                     else
    411.  
    412.                     {                  
    413.  
    414.                         // Change the location of the joystick graphic to match where the touch is
    415.  
    416.                         tmprect = gui.pixelInset;
    417.  
    418.                         tmprect.x = Mathf.Clamp( guiTouchPos.x, guiBoundary.min.x, guiBoundary.max.x );
    419.  
    420.                         tmprect.y = Mathf.Clamp( guiTouchPos.y, guiBoundary.min.y, guiBoundary.max.y );    
    421.  
    422.                         gui.pixelInset = tmprect;
    423.  
    424.                     }
    425.  
    426.                    
    427.  
    428.                     if ( touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled )
    429.  
    430.                         ResetJoystick();                    
    431.  
    432.                 }          
    433.  
    434.             }
    435.  
    436.         }
    437.  
    438.        
    439.  
    440.         if ( !touchPad )
    441.  
    442.         {
    443.  
    444.             // Get a value between -1 and 1 based on the joystick graphic location
    445.  
    446.             position.x = ( gui.pixelInset.x + guiTouchOffset.x - guiCenter.x ) / guiTouchOffset.x;
    447.  
    448.             position.y = ( gui.pixelInset.y + guiTouchOffset.y - guiCenter.y ) / guiTouchOffset.y;
    449.  
    450.         }
    451.  
    452.        
    453.  
    454.         // Adjust for dead zone
    455.  
    456.         float absoluteX = Mathf.Abs( position.x );
    457.  
    458.         float absoluteY = Mathf.Abs( position.y );
    459.  
    460.        
    461.  
    462.         if ( absoluteX < deadZone.x )
    463.  
    464.         {
    465.  
    466.             // Report the joystick as being at the center if it is within the dead zone
    467.  
    468.             position.x = 0;
    469.  
    470.         }
    471.  
    472.         else if ( normalize )
    473.  
    474.         {
    475.  
    476.             // Rescale the output after taking the dead zone into account
    477.  
    478.             position.x = Mathf.Sign( position.x ) * ( absoluteX - deadZone.x ) / ( 1 - deadZone.x );
    479.  
    480.         }
    481.  
    482.            
    483.  
    484.         if ( absoluteY < deadZone.y )
    485.  
    486.         {
    487.  
    488.             // Report the joystick as being at the center if it is within the dead zone
    489.  
    490.             position.y = 0;
    491.  
    492.         }
    493.  
    494.         else if ( normalize )
    495.  
    496.         {
    497.  
    498.             // Rescale the output after taking the dead zone into account
    499.  
    500.             position.y = Mathf.Sign( position.y ) * ( absoluteY - deadZone.y ) / ( 1 - deadZone.y );
    501.  
    502.         }
    503.  
    504.     }
    505.  
    506.  
    507.  
    508. }
     
  12. Deleted User

    Deleted User

    Guest

    kcirdnehnosaj im trying to use your updated Script with my TouchZone gets changed to a funky negative one.. :/

    Im using Pixel Inset (-200,0,200,125)

    The standard js script would change Touch Zone X too = 905.. While yours changes it to -200... How would i change that?
     
    Last edited by a moderator: Aug 11, 2012
  13. geniuscd

    geniuscd

    Joined:
    Aug 10, 2012
    Posts:
    23
    hey guys, I can confirm that it is working on the iphone and Ipad after testing it directly to the devices.
    thank you...
    please follow the tutorial in order to know how to assign the script.
     
  14. blaze

    blaze

    Joined:
    Dec 21, 2011
    Posts:
    211
    Thank you very much for the C# Script. It working very well!
    I created a repository in GitHub with the script, a prefab and the Angry Bot's "ThumbStickPad" texture.
    Any improvement that I will made, I will push there.

    http://goo.gl/DJEpJ

    Thanks again!
     
    Last edited: Sep 14, 2012
  15. nbg_yalta

    nbg_yalta

    Joined:
    Oct 3, 2012
    Posts:
    378
    How to avoid this warnings? Can I can remove this variables?
     
    Last edited: Feb 14, 2013
  16. Michieal

    Michieal

    Joined:
    Jul 7, 2013
    Posts:
    92
    you do not want to remove the variables... these warnings occur because you 1st) set the variable to a value... then, 2nd) only use it in an if(variable == ...) statement. The variables are being used, and for a proper purpose. The warnings are erroneous, and should be disregarded / ignored.

    but, if you really want to remove them, put the following code piece in, for each variable... it should trigger the "oh, these really are being used..." in the compiler...

    float myval = FloatVarToClear;
    FloatVarToClear = myval;


    by assigning them to a temp variable, and then back, it should clear the warnings, at the expense of proccessing cycles. Note, I did warn you that it would eat up processor cycles, just to make the harmless warnings go away.
     
  17. VAN-D00M

    VAN-D00M

    Joined:
    Dec 24, 2013
    Posts:
    44


    I get this error ' error CS0119: Expression denotes a `variable', where a `method group' was expected' on line 159.

    if ( shouldLatchFinger ( lastFingerId == -1 || lastFingerId != touch.fingerId ) )
     
    sush333 likes this.
  18. sush333

    sush333

    Joined:
    Jan 26, 2015
    Posts:
    32
    Hey Van, i am also getting this error. did you finf the solution ? or perhaps you can tell me how to use the default joystick.js ?
     
  19. uzairamirs

    uzairamirs

    Joined:
    Jan 7, 2015
    Posts:
    8



    Quite late though, almost a year :p

    replace this line with if ( shouldLatchFinger && ( lastFingerId == -1 || lastFingerId != touch.fingerId ) ) or just add && in between shouldLatchFinger and the "("
     
  20. ZikGameProduction

    ZikGameProduction

    Joined:
    Dec 9, 2016
    Posts:
    1
    How to convert code to UnityEngine.UI?
    Current versions of Unity 3d do not support pixelInset, HitTest ...