Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Double Jump not working , lost

Discussion in 'Scripting' started by Darktower121, Sep 11, 2020.

  1. Darktower121

    Darktower121

    Joined:
    Aug 27, 2020
    Posts:
    5
    Been trying to fix this for a while now and I can't figure out why this isn't working. Trying to jump just the one more time while in the air from a regular jump. I'm new to coding and Unity, any help would be awesome.


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UIElements;
    5.  
    6. public class PlayerMovement : MonoBehaviour
    7. {
    8.     public float speed;              
    9.     public Rigidbody2D rb;            
    10.     float moveHorizontal;
    11.     public float jumpUp;
    12.     bool jumpKeyWasPressed;
    13.     public bool isGrounded;
    14.     public bool jumpBoost;
    15.     public bool dashActivator;
    16.     bool dashButton;
    17.     public Transform dashSpotRef;
    18.     public float dashSpeed;
    19.     bool canDoubleJump;
    20.    
    21.  
    22.  
    23.    
    24.    
    25.    
    26.  
    27.     public void Start()
    28.     {
    29.         jumpBoost = false;
    30.         dashActivator = false;
    31.         dashButton = false;
    32.        
    33.     }
    34.  
    35.     private void OnCollisionEnter2D(Collision2D grounderChecker)
    36.     {
    37.         if (grounderChecker.gameObject.CompareTag("Ground"))
    38.         {
    39.             isGrounded = true;
    40.  
    41.         }
    42.     }
    43.     private void OnCollisionExit2D(Collision2D grounderChecker)
    44.     {
    45.         if (grounderChecker.gameObject.CompareTag("Ground"))
    46.         {
    47.             isGrounded = false;
    48.         }
    49.     }
    50.      void Update()
    51.     {
    52.         moveHorizontal = Input.GetAxis("Horizontal");
    53.  
    54.    
    55.  
    56.         if (Input.GetButtonDown("Jump") && isGrounded == true )
    57.         {
    58.  
    59.             jumpKeyWasPressed = true;
    60.            
    61.          
    62.         }
    63.         if(Input.GetButtonDown("Fire1"))
    64.         {
    65.             dashButton = true;
    66.         }
    67.        
    68.         if (jumpBoost == true)
    69.         {
    70.             jumpUp = 12;
    71.             Invoke("Falser", 5);
    72.         }
    73.        
    74.  
    75.  
    76.  
    77.     }
    78.     void FixedUpdate()
    79.     {
    80.         rb.velocity = new Vector2(moveHorizontal * speed, rb.velocity.y);
    81.  
    82.  
    83.         if (jumpKeyWasPressed == true)
    84.         {
    85.  
    86.             rb.velocity = Vector2.up * jumpUp;//jump
    87.             jumpKeyWasPressed = false; // super important to set bool back to false
    88.             canDoubleJump = true;
    89.         }
    90.         else
    91.         {
    92.             if (canDoubleJump && Input.GetButtonDown("Jump"))
    93.             {
    94.                
    95.                 rb.velocity = Vector2.up * jumpUp;
    96.                 canDoubleJump = false;
    97.             }
    98.  
    99.         }
    100.  
    101.        
    102.        
    103.            
    104.            
    105.            
    106.         if( dashButton == true)
    107.         {
    108.            
    109.  
    110.             rb.velocity = new Vector2(moveHorizontal * dashSpeed, rb.velocity.y);// blink to postion in front on the X you are moving towards
    111.  
    112.             Invoke("SlowDown", .01f);
    113.  
    114.         }
    115.         if(dashButton == true)
    116.             if(moveHorizontal == 0)
    117.             {
    118.                 rb.velocity = new Vector2(1 * dashSpeed, rb.velocity.y);// blink to postion in front when not moving
    119.  
    120.                 Invoke("SlowDown", .01f);
    121.             }
    122.     }
    123.     void SlowDown()
    124.     {
    125.         dashButton = false;
    126.     }
    127.     void Falser()//sets bool back to false after time ends for power up
    128.     {
    129.         jumpBoost = false;
    130.         jumpUp = 6;
    131.     }
    132.  
    133.  
    134.  
    135.  
    136. }
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    You don't want to check for Input in FixedUpdate. FixedUpdate may not always catch the input. This is possibly your issue.
     
    Darktower121 likes this.