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

How to make character JUMP?

Discussion in 'iOS and tvOS' started by webphone, Jun 15, 2010.

  1. webphone

    webphone

    Joined:
    Mar 13, 2010
    Posts:
    315
    I use Camera Relative Controls and it can control to move left and right.

    How can I make the character to:
    1. Jump vertically
    2. Jump forward and backward

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

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    PlayerRelativeControl.js (also in the Penelope tutorial) implements a jump when the player double taps on the "joystick".
     
  3. webphone

    webphone

    Joined:
    Mar 13, 2010
    Posts:
    315
    I downloaded Penelope and opened EmeraciteMine.unity file under Completed Project (just double click it). Then, unity basic opened it.

    1) http://unity3d.com/support/resources/tutorials/penelope

    "how to make a 3rd person game in Unity iPhone" - should I open it in unity iphone instead of opening it in unity basic? Do I create a new project and import it?

    2) How to fix those errors in the attached image?
     

    Attached Files:

  4. webphone

    webphone

    Joined:
    Mar 13, 2010
    Posts:
    315
    Dear Sir

    I created a new project in unity iphone and imported
    part3
    PenelopeArtwork
    theOrbs

    unityPackage

    I started unity remote and able to see the scene.

    However, nothing can move when i touch the screen.

    Did I miss some files or did wrong procedure?
     

    Attached Files:

  5. webphone

    webphone

    Joined:
    Mar 13, 2010
    Posts:
    315
    Dear Sir

    I can deploy it to ipod touch

    And, I will look at those sample code.

    The camera relative is what did I want.

    Thanks for great help