Search Unity

Double Jump Problem

Discussion in 'Scripting' started by simone9725, Nov 29, 2015.

  1. simone9725

    simone9725

    Joined:
    Jul 19, 2014
    Posts:
    234
    Hi,
    I'm writing a code for my platformer game .I have an issue with double jump .When I jump after 3 jump I'm not able to jump anymore also when I collide with object in scene.
    My code is:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerController : MonoBehaviour {
    5.     public float moveSpeed;
    6.     public float jumpHeight;
    7.     private Rigidbody2D rigi;
    8.     public Transform groundCheck;
    9.     public float groundCheckRadius;
    10.     public LayerMask whatIsGround;
    11.     private bool grounded;
    12.     private bool doubleJumped;
    13.     // Use this for initialization
    14.     private void Awake()
    15.     {
    16.         rigi = GetComponent<Rigidbody2D>();
    17.     }
    18.     void Start () {
    19.    
    20.     }
    21.     void FixedUpdate () {
    22.         grounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius,whatIsGround);
    23.                     }
    24.     // Update is called once per frame
    25.     void Update () {
    26.         if (grounded)
    27.                         doubleJumped = false;
    28.             if(Input.GetKeyDown (KeyCode.Space) && grounded)
    29.             {
    30.             //rigi.velocity = new Vector2(rigi.velocity.x, jumpHeight);
    31.             Jump ();
    32.         }
    33.         if(Input.GetKeyDown (KeyCode.Space) && !doubleJumped && !grounded)
    34.         {
    35.             //rigi.velocity = new Vector2(rigi.velocity.x, jumpHeight);
    36.             Jump();
    37.  
    38.             doubleJumped=true;
    39.         }
    40.         if(Input.GetKey (KeyCode.RightArrow))
    41.         {
    42.             rigi.velocity = new Vector2(moveSpeed, rigi.velocity.y);
    43.         }
    44.         if(Input.GetKey (KeyCode.LeftArrow))
    45.         {
    46.             rigi.velocity = new Vector2(-moveSpeed, rigi.velocity.y);
    47.         }
    48.     }
    49.     public void Jump(){
    50.         rigi.velocity = new Vector2(rigi.velocity.x, jumpHeight);
    51.     }
    52. }
    Thanks :)
     
  2. Comafly

    Comafly

    Joined:
    May 30, 2014
    Posts:
    87
    Is it ALWAYS after 3 jumps? Doesn't seem to be any issue with your code. All I can think of is that maybe your groundCheck.position isn't hitting your ground properly, so it's not setting doubleJumped back to false.

    Trying doing a Debug.Log(doubleJumped); on your Update function - this will output the value of doubleJumped to the console window, and see if doubleJumped is being properly set back to false every time.