Search Unity

Help with creating a double jump script? (C#)

Discussion in 'Scripting' started by milliehashh, Feb 12, 2016.

  1. milliehashh

    milliehashh

    Joined:
    Sep 17, 2015
    Posts:
    72
    I'm trying to create a simple double jump script but i don't really know where to start, ive looked at a lot of tutorials but they seem to clash with my current code - can anyone recommend a simple way of me doing this? here is my code:
    Code (CSharp):
    1.     public bool grounded = false;
    2.     public float moveSpeed = 10f;
    3.     public int maxjump = 2;
    4.     public int jumps = 0;
    5.  
    6.  
    7.     public Transform lineStart, lineEnd, groundedEnd;
    8.  
    9.     void Start(){
    10.        
    11.     }
    12.  
    13.     void FixedUpdate(){
    14.         Movement ();
    15.         raycasting ();
    16.  
    17.     }
    18.  
    19.  
    20.     void raycasting(){
    21.         Debug.DrawLine (lineStart.position, lineEnd.position, Color.green);
    22.         grounded = Physics2D.Linecast (this.transform.position, groundedEnd.position, 1 << LayerMask.NameToLayer ("Ground"));
    23.        
    24.        
    25.     }
    26.  
    27.     void Movement(){
    28.  
    29.         if (Input.GetKey (KeyCode.D)){
    30.             transform.Translate (new Vector3 (1, 0, 0) * moveSpeed * Time.deltaTime);
    31.         }
    32.  
    33.         if (Input.GetKey (KeyCode.A)){
    34.             transform.Translate (new Vector3 (-1, 0, 0) * moveSpeed * Time.deltaTime);
    35.         }
    36.  
    37.         if (Input.GetKey (KeyCode.W) && grounded == true){
    38.             GetComponent<Rigidbody2D>().AddForce(Vector3.up * 320);
    39.  
    40.  
    41.         }
    42.     }
    43. }
    44.  
    45.        
    ive set up some integers as a start but i don't really know what to do after that - is the best way to do this by adding 1 to the 'jumps' integer every time the player jumps? if so how do i write it so that when the 'maxjump' integer is equal to 2 the player can no longer jump?
    thanks for any help in advance, i know I'm asking quite a lot.:)
     
  2. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    so the initial thought, was that you need to have a variable store if you could double jump and only reset if you were grounded. It would not become false if you were grounded and pressed the jump key.

    Now, also, if you are doing this correctly, dont use Input.GetKey() as this will always be correct for X number of fixed updates until the next update happened. So using Input.GetKeyDown() only will trigger once per key press.

    Next, I would make use of that rigid body you are working with. You are pretty much only using transform.Translate, but that doesnt work too well with a rigidbody. I would use the rigidbody and set the velocity directly. This way you let the physics engine work for you.

    Code (csharp):
    1.  
    2.     private Rigidbody rigidbody;
    3.     private bool grounded;
    4.     public float speed = 8;
    5.     public float jumpSpeed = 7;
    6.     public bool canDoubleJump = false;
    7.     private float movement = 0;
    8.  
    9.     void Start() {
    10.         // preset the rigidbody
    11.         // perhaps require it on this script so it will always be there
    12.         rigidbody = GetComponent<Rigidbody>();
    13.     }
    14.  
    15.     public bool isGrounded() {
    16.         Vector3 end = transform.position + Vector3.down * 1.1f;
    17.         grounded = Physics2D.Linecast(transform.position, end, 1 << LayerMask.NameToLayer("Ground"));
    18.         return grounded;
    19.     }
    20.     void Movement() {
    21.         // set grounded
    22.         isGrounded();
    23.  
    24.         // collect the horizontal input
    25.         float h = Input.GetAxis("Horizontal") * Time.fixedDeltaTime;
    26.         // if we are not grounded, modify the input to slowly react to the change.
    27.         if (!grounded) {
    28.             h = Mathf.Lerp(movement, h, 5 * Time.fixedDeltaTime);
    29.         }
    30.         // store the movement back
    31.         movement = h;
    32.  
    33.         // adjust the velocity
    34.         Vector3 velocity = rigidbody.velocity;
    35.         velocity.x = h * speed;
    36.  
    37.         // if we are grounded, reset canDoubleJump
    38.         if (grounded)
    39.             canDoubleJump = true;
    40.  
    41.         // if we are grounded, or we can double jump, see if we have pressed the jump key
    42.         if ((grounded || canDoubleJump) && Input.GetKeyDown(KeyCode.W)) {
    43.             velocity.y = jumpSpeed;
    44.             if (!grounded) canDoubleJump = false;
    45.         }
    46.  
    47.         // set the velocity back to the rigid body
    48.         rigidbody.velocity = velocity;
    49.     }
    50.  
     
  3. Browdaddy96

    Browdaddy96

    Joined:
    Aug 27, 2015
    Posts:
    82
    Personally I run jumping off a series of bools like grounded and doubleJump.
    Code (CSharp):
    1. bool grounded;
    2. bool doubleJump;
    3.  
    4. void Update()
    5. {
    6.     if(!grounded)
    7.          doubleJump = true;
    8.     else
    9.     {
    10.           doubleJump = false;
    11.     }
    12. }
    13.  
    14. void Movement()
    15. {
    16.      //movement stuff
    17.  
    18.      if(doubleJump && *Insert your jump key thing here*) {
    19.           GetComponent<Rigidbody2D>().AddForce(Vector3.up * 280);//less because a double jump looks nicer when it isn't as big as a full blown jump.
    20. }
    Should work but I'm in school so I don't know.
     
  4. milliehashh

    milliehashh

    Joined:
    Sep 17, 2015
    Posts:
    72
    hey, thanks for getting back to me - yep that was my initial plan for this. :) ah okay, thanks for letting me know, ill keep that in mind in the future.

    when i put this script in i got an warning message in return, is this something that could cause a problem?
    if i ignored the error message and tried to play the script anyways the character won't jump or move, not sure if this is me doing something wrong or not though.

    and also thanks for your response too Browdaddy96 but this doesn't seem to allow my character to double jump, i put in a print function to test this just after the if statement and it doesn't print. I'm sat in college working on this right now so no worries, i understand haha.

    also don't worry about this as this is just an extra i am working on for practice, i will also carry on researching this in my spare time. :)
     
  5. milliehashh

    milliehashh

    Joined:
    Sep 17, 2015
    Posts:
    72
    ^^my script is called testing by the way. thats why its "testing.rigidbody".
     
  6. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    re-post the entire script please.
     
  7. Browdaddy96

    Browdaddy96

    Joined:
    Aug 27, 2015
    Posts:
    82
    First set its velocity to zero using rigidbody2D. Velocity = 0 then do the double jump.
    They will stop the downward fall.