Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Double Jump in C# (It's really bad)

Discussion in 'Scripting' started by Tacosaurus, Mar 12, 2015.

  1. Tacosaurus

    Tacosaurus

    Joined:
    Mar 5, 2015
    Posts:
    8
    I need some help with this. I've barely had any sleep, so I just need help with making this look better and actually work.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Movement : MonoBehaviour {
    5.  
    6.     public float spd = 6.0f;
    7.     public float jumpSpeed = 40.0f;
    8.     public float grav = 550.0f;
    9.     private bool isGrounded = true;
    10.     private bool dblJump = false;
    11.  
    12.     Vector3 moveDirection = new Vector3();
    13.  
    14.     // Update is called once per frame
    15.     void FixedUpdate () {
    16.         JetPack();
    17.         print(isGrounded);
    18.     }
    19.     void OnCollisionEnter(Collision col){
    20.         isGrounded = true;
    21.     }
    22.  
    23.     void JetPack(){
    24.         CharacterController controller = GetComponent<CharacterController>();
    25.             /*We are grounded to start off, so recalculate
    26.             move direction directly from axes*/
    27.             moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
    28.             moveDirection = transform.TransformDirection(moveDirection);
    29.             moveDirection *= spd;
    30.            
    31.             if (Input.GetKeyDown(KeyCode.Space)){
    32.                 if (isGrounded){
    33.                     moveDirection.y = jumpSpeed;
    34.                     dblJump = true;
    35.  
    36.                     if (Input.GetKeyUp(KeyCode.Space)){
    37.  
    38.                         if (!isGrounded && dblJump == true){
    39.  
    40.                             if (Input.GetKey(KeyCode.Space)){
    41.                                 moveDirection.y = jumpSpeed;
    42.                                 dblJump = false;
    43.                             }
    44.                         }
    45.                     }
    46.                 }
    47.             }
    48.        
    49.         //Apply gravity
    50.         moveDirection.y -= grav * Time.deltaTime;
    51.        
    52.         //Move the controller
    53.         controller.Move (moveDirection * Time.deltaTime);
    54.     }
    55. }
     
  2. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    You should be checking for your second jump when you're "not" grounded...you're checking getkeyup when you're grounded...After jumping, you're not grounded anymore, thus it won't work.
     
  3. PJRM

    PJRM

    Joined:
    Mar 4, 2013
    Posts:
    303
    Tacosaurus, what is wrong? any erros!?
    looking at coherence of "double jump" you might want this:
    Code (CSharp):
    1.  
    2. if (Input.GetKeyDown(KeyCode.Space)) {
    3.   if (isGrounded){
    4.     moveDirection.y = jumpSpeed;
    5.     dblJump = true;
    6.   } else {
    7.     // use here to check the second jump "double"
    8.     if (Input.GetKeyUp(KeyCode.Space) && (dblJump == true)) {
    9.       moveDirection.y = jumpSpeed;
    10.       dblJump = false;
    11.     }
    12.   }
    13. }
    remember the concept of loop.... this code will always be calling by the engine, so
    • first run: you jump, and you still grounded (impossible to check for the second jump grounded)
    • second run: you in the air, so check for the second jump
     
  4. Tacosaurus

    Tacosaurus

    Joined:
    Mar 5, 2015
    Posts:
    8
    Thanks for the input guys. I think I'll just re-code it from scratch, and make sure to keep it simple and easy.