- Home /
              This post has been wikified, any user with enough reputation can edit it. 
            
 
             
               Question by 
               tonio1206 · Jan 28, 2015 at 07:21 AM · 
                2djavascriptgamejump  
              
 
              Problem with jumping
I am currently making a 2D game but I have a problem with jumping. I want my character to be able to jump only one time before he is grounded again. Here is my script:
     public var jumpPower : float = 1000.0f;
     private var canJump : boolean = true;
         
     function Update()
     {
         if (Input.GetKeyDown("w"))
             {
                    if(canJump == true)
                 {
                     rigidbody2D.AddForce (transform.up * jumpPower);
                     canJump = false;
                 }
         
                 
             }
         }
         
         
      function OnCollisionEnter2D(coll: Collision2D)
         {
             if(coll.gameObject.tag == "Ground")
             {
                 canJump = true;
             }
         }
         
     
The cubes ,on which the player lands, are tag as "Ground".
               Comment
              
 
               
              Answer by SmileyMerx · Jan 28, 2015 at 11:41 AM
There are multiple tutorials, here is an easy one with mid-air movement (left/right):
 using UnityEngine;
 using System.Collections;
 
 public class MovementWorm3 : MonoBehaviour {
     
     float speed     = 6.0f;
     float jumpSpeed = 8.0f;
     float gravity   = 20.0f;
 
     private Vector3 moveDirection = Vector3.zero;
     private CharacterController controller;
 
     // Use this for initialization
     void Start () {
         controller = GetComponent<CharacterController>();    
     }
     
     // Update is called once per frame
     void Update () {
             
         if (controller.isGrounded) {
             // We are grounded, so recalculate
             // move direction directly from axes        
             moveDirection = new Vector3(Input.GetAxis ("Horizontal"), 0, 0);
             moveDirection = transform.TransformDirection(moveDirection);
             moveDirection = moveDirection * speed;
 
             if (Input.GetButton ("Jump")) {
                 moveDirection.y = jumpSpeed;
             }
         }
         else {
             // Apply right/left, if we are NOT grounded
             // --> Left/Right movement in mid air allowed
             moveDirection.x = Input.GetAxis ("Horizontal") * speed;
         }
 
         // Apply gravity
         moveDirection.y -= gravity * Time.deltaTime;
         
         // Move the controller
         controller.Move(moveDirection * Time.deltaTime);
     }
 }
Your answer
 
 
             Follow this Question
Related Questions
Touching moving sprites 0 Answers
2D enemy AI 1 Answer
2D Jump AI Help 0 Answers
Player movement boudaries in 2D 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                