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

Please... help with jump buffering

Discussion in '2D' started by John_Briscoe, Apr 14, 2020.

  1. John_Briscoe

    John_Briscoe

    Joined:
    Apr 12, 2020
    Posts:
    2
    I'm still really new to unity and i'm struggling to get a good play movement feel.
    Basically, I want to implement jump buffering where if the player has yet to touch the ground but presses jump, there is a grace period that allows them to jump. possibly 5 frames or so or make it so I can easily edit the jump buffer time in a serializedfield? I'm still learning it all. Please, any help is greatly appreciated.
    Code (CSharp):
    1. public class Player : MonoBehaviour
    2.  
    3.     //Config / anything to do with setting our variables
    4. {
    5.     [SerializeField] float runSpeed = 5f;
    6.     [SerializeField] float jumpSpeed = 5f;
    7.     [SerializeField] float climbSpeed = 5f;
    8.     [SerializeField] Vector2 deathkick = new Vector2(25f, 25f);
    9.  
    10.  
    11.     //State
    12.     bool isAlive = true;
    13.  
    14.     // Cached component references
    15.  
    16.     Rigidbody2D myRigidBody;
    17.     Animator myAnimator;
    18.     CapsuleCollider2D myBodyCollider;
    19.     BoxCollider2D myFeet;
    20.     float gravityScaleAtStart;
    21.  
    22.  
    23.  
    24.     // Message then Methods
    25.  
    26.  
    27.     void Start()
    28.     {
    29.         myRigidBody = GetComponent<Rigidbody2D>();
    30.         myAnimator = GetComponent<Animator>();
    31.         myBodyCollider = GetComponent<CapsuleCollider2D>();
    32.         gravityScaleAtStart = myRigidBody.gravityScale;
    33.         myFeet = GetComponent<BoxCollider2D>();
    34.     }
    35.  
    36.     // Update is called once per frame
    37.     void Update()
    38.     {
    39.         if (!isAlive) { return; }
    40.         Run();
    41.         FlipSprite();
    42.         Jump();
    43.         ClimbLadder();
    44.         Die();
    45.  
    46.     }
    47.     private void Run ()
    48.     {
    49.         float controlThrow = CrossPlatformInputManager.GetAxis("Horizontal"); // value is between +1 and -1
    50.         Vector2 playerVelocity = new Vector2(controlThrow * runSpeed, myRigidBody.velocity.y);
    51.         myRigidBody.velocity = playerVelocity;
    52.  
    53.         bool playerHasHorizontalSpeed = Mathf.Abs(myRigidBody.velocity.x) > Mathf.Epsilon;
    54.         myAnimator.SetBool("Running", playerHasHorizontalSpeed);
    55.        
    56.     }
    57.  
    58.     private void ClimbLadder()
    59.     {
    60.         if(!myFeet.IsTouchingLayers(LayerMask.GetMask("Climbing")))
    61.         {
    62.             myAnimator.SetBool("Climbing", false);
    63.             myRigidBody.gravityScale = gravityScaleAtStart;
    64.             return;
    65.         }
    66.  
    67.         float controlThrow = CrossPlatformInputManager.GetAxis("Vertical");
    68.         Vector2 climbVelocity = new Vector2(myRigidBody.velocity.x, controlThrow * climbSpeed);
    69.         myRigidBody.velocity = climbVelocity;
    70.         myRigidBody.gravityScale = 0f;
    71.  
    72.         bool playerHasVerticalSpeed = Mathf.Abs(myRigidBody.velocity.y) > Mathf.Epsilon;
    73.         myAnimator.SetBool("Climbing", playerHasVerticalSpeed);
    74.     }
    75.  
    76.     private void Jump()
    77.     {
    78.         if (!myFeet.IsTouchingLayers(LayerMask.GetMask("Ground"))) { return; }
    79.  
    80.         if (CrossPlatformInputManager.GetButtonDown("Jump"))
    81.         {
    82.             Vector2 jumpVelocityToAdd = new Vector2(0f, jumpSpeed);
    83.             myRigidBody.velocity += jumpVelocityToAdd;
    84.         }
    85.     }
    86.  
    87.     private void Die()
    88.     {
    89.         if (myBodyCollider.IsTouchingLayers(LayerMask.GetMask("Enemy", "Hazzards")))
    90.         {
    91.             isAlive = false;
    92.             myAnimator.SetTrigger("Dying");
    93.             GetComponent<Rigidbody2D>().velocity = deathkick;
    94.             FindObjectOfType<GameSession>().ProcessPlayerDeath();
    95.         }
    96.     }
    97.  
    98.  
    99.     private void FlipSprite()
    100.     {
    101.         bool playerHasHorizontalSpeed = Mathf.Abs(myRigidBody.velocity.x) > Mathf.Epsilon;
    102.         if (playerHasHorizontalSpeed)
    103.         {
    104.             transform.localScale = new Vector2(Mathf.Sign(myRigidBody.velocity.x), 1f);
    105.         }
    106.     }
    107. }
     
  2. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,353
    Check out this GDC talk, it will assist you with the idea. At minute 8:30 he talks about Early Input Forgiveness


    The whole talk is great, he also goes over Late Input Forgiveness before that point as well.
     
    John_Briscoe likes this.
  3. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    Hi @John_Briscoe

    Google youtuber GamesPlusJames, he just recently did a video about it, including other similar topics like late jump.
     
    LeoFeitosa and John_Briscoe like this.
  4. John_Briscoe

    John_Briscoe

    Joined:
    Apr 12, 2020
    Posts:
    2
    Thanks you guys are legends :)
    @eses i believe you’re referring to his 5 ways to make a better platformer and indeed he puts code examples and it’s literally exactly what I needed! Thanks so much I can’t believe his video didn’t come up through all my searching
     
    Cornysam likes this.
  5. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,353
    Just a little extra, but here are all the video game tutorial pages or game dev pages I subscribe to on YouTube. All of these have helped me in one way or another.

    upload_2020-4-15_9-16-58.png
     
    LeoFeitosa likes this.