- Home /
 
Jump Script for 2D (JavaScript)
I am trying to add some code to my android game that lets you jump when swiping up. I have already made the swipe script and I need a jump code to go where I put the comment "code for jump". Plz help.
 #pragma strict
 var player : Transform;  // Drag your player here
 private var fp : Vector2;  // first finger position
 private var lp : Vector2;  // last finger position
 
 function Update()
 {
    for (var touch : Touch in Input.touches)
    {
          if (touch.phase == TouchPhase.Began)
          {
                fp = touch.position;
                lp = touch.position;
          }
          if (touch.phase == TouchPhase.Moved )
          {
                lp = touch.position;
          }
          if(touch.phase == TouchPhase.Ended)
          { 
               Debug.Log("Wrong swipe direction.");
          }
          else if((fp.x - lp.x) < -80) // right swipe
          {
              Debug.Log("Wrong swipe direction."); 
          }
          else if((fp.y - lp.y) < -80 ) // up swipe
          {
                 // code for jump
                 
          }
     }
 }
 
              do you have rigidbody? if you do , just usee AddForce. Otherwise , if you dont want to use a rigidbody, you have to do it with transform.Translate but you will also need to add a costant downward acceleration for gravity
I have been using the roll a ball prefap that came with the standard assets for mobile. I think it would be easier to do it with transform.Translate. When I tried it the jump did not work. Would the rigidbody be interfering with it?
if you have a rigidbody just do what i said before, in the jump part of your code use addforce and make sure gravity is enabled on your prefab
I have sorted it now. When I switched it from 2D to 3D it worked.
Your answer
 
             Follow this Question
Related Questions
How can I move player sideways by dragging with mouse or with mobile device when pressed down? 0 Answers
How to Move the Car With Touch? 1 Answer
How to touch, move and rotate 3D objects in XZ? 1 Answer
How do i need to change this script for mobile? 1 Answer
Unity 2D Mobile Game Drawing Mechanic 0 Answers