Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
211 views
in Technique[技术] by (71.8m points)

c# - Need help fixing this jump buffer (Problem: Player occasionally double jumps)

I tried to make a simple Jump buffer and Coyote timer. As you can see, sometimes the player does a double jump, but only if the jump button is clicked very fast.

As you can see <here>, it only happens a few times.

[Header("Player Accesiblility")]
 public float hangTime = 1.5f;
 [SerializeField]
 private float hangCounter;
 [SerializeField]
 private float jumpTimer;
 public float jumpDelay = 0.25f;

private void FixedUpdate()
 {
     if (jumpTimer > Time.time && coll.onGround)
     {
         Jump(Vector2.up, false);
     }
 }
Void update(){

 if (coll.onGround) //Coyote Time for Jump
     {
         hangCounter = hangTime;
     }

     if (Input.GetButtonDown("Jump")) //Jump Function
      {
         anim.SetTrigger("jump");

         jumpTimer = Time.time + jumpDelay;
         if (!coll.onGround && hangCounter > 0)
         {
             Jump(Vector2.up, false);
             hangCounter = 0;
         }


         if (coll.onWall && !coll.onGround)
          {

             WallJump();
          }
      }
}
 private void Jump(Vector2 dir, bool wall)
  {
     rb.velocity = new Vector2(rb.velocity.x, 0);
      rb.velocity += dir * jumpForce;
     jumpTimer = 0f;
     hangCounter = 0;
  }

Any help will be much appreciated, and thank you for your time.

question from:https://stackoverflow.com/questions/65847846/need-help-fixing-this-jump-buffer-problem-player-occasionally-double-jumps

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I got an answer from @Mateusz that helped me. His comment: It's mostly likely because you're doing physics in Update and not in FixedUpdate. Read this to know more (https://docs.unity3d.com/ScriptReference/MonoBehaviour.FixedUpdate.html)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...