Coyote Time & Jump Buffering In Unity

Coyote Time & Jump Buffering In Unity

bendux

2 года назад

76,408 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

@KHJohan
@KHJohan - 05.12.2023 14:43

Im working in Godot but this is still helpful. Consider naming your videos “coyote time logic” or “jump buffering code method (example in Unity)” so you can reach a wider audience :)

Ответить
@GabrielNunes-kx2hv
@GabrielNunes-kx2hv - 28.11.2023 00:02

this lines makes it possible to jump infinitely:
rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * 0.5f);

So remove it. And yeah, it is from the link on the description

Ответить
@cerf14506
@cerf14506 - 20.11.2023 15:59

I advise you to learn about coroutines

Ответить
@har3nz
@har3nz - 13.11.2023 21:35

but you can double jump now bcs of jump buffering now if you spam jump pls help

Ответить
@praevasc4299
@praevasc4299 - 08.11.2023 19:25

It irks me just a little that there is no underflow protection. Of course you'd need to let the game running without touching it for a ludicrously inordinate time for it to become a problem, but it would still be a good habit to handle it.

Ответить
@Studd5
@Studd5 - 18.10.2023 13:31

how do you jump

Ответить
@VanStabHolme
@VanStabHolme - 18.10.2023 01:03

Such simple and elegant implementation. I had 0 problems making it work with Love2D, thank you for help!

Ответить
@hiimtatsugo7341
@hiimtatsugo7341 - 13.10.2023 20:22

anyone had the problem with if you press jump really fast the charater now can jump three time instead of two(double jump).Here is my code:
void Jump()
{
if (Input.GetButtonDown("Jump"))
{
jumpPressRemember = jumpPressRememberTime;
}
if (isGrounded || isFacingWall)
{
groundedRemember = groundedRememberTime;
}

if(jumpPressRemember > 0)
{
if (groundedRemember > 0)
{
jumpPressRemember = 0;
groundedRemember = 0;
rb.velocity = new Vector2(rb.velocity.x, jumpPower);
doubleJump = true;
dust.Play();
Debug.Log("Jump1");
}
else if (doubleJump)
{
jumpPressRemember = 0;
rb.velocity = new Vector2(rb.velocity.x, jumpPower);
doubleJump = false;
dust.Play();
Debug.Log("Jump2");
}
}
}

Ответить
@arneneshjonnevag8722
@arneneshjonnevag8722 - 02.10.2023 20:13

I don't think hollow knight have coyote time

Ответить
@AchimTheEagle
@AchimTheEagle - 23.09.2023 22:47

The best and easiest solution I've seen so far! 10/10

Ответить
@maksy71
@maksy71 - 23.09.2023 22:05

Thank you so much ❤, you are a live-saver❤

Ответить
@user-bf7xm5bo1d
@user-bf7xm5bo1d - 08.09.2023 13:05

Physics2D.OverlapCircle(groundCheck.position, 0.2f, groundLayer);

this 0.2f jumpBufferTime ?

Ответить
@yll1b596
@yll1b596 - 26.08.2023 21:21

Such a good tutorial! Thanks!

Ответить
@David-Obermiller
@David-Obermiller - 22.08.2023 05:36

I am using the new input system and invoking my jump with unity events. I can't for the life of me figure out how to implement jump buffering in this way. Every single tutorial uses the old input system. Can someone PLEASE help!

Ответить
@usamabinmuzaffar692
@usamabinmuzaffar692 - 02.08.2023 12:18

Is there a way to get the jump buffer to work with the new input system? I got the coyote timer to work but I can't seem to get the jump buffer right.

Ответить
@NathanPrince1
@NathanPrince1 - 29.07.2023 02:43

this is amazing thanks for the simple but efficient code works amazing

Ответить
@corterri
@corterri - 17.07.2023 03:49

Little details like this are what give video games that smooth gameplay feeling.

Ответить
@logical-game-dev
@logical-game-dev - 13.07.2023 16:45

thanks :)

Ответить
@ytbrowse6039
@ytbrowse6039 - 30.06.2023 19:14

more mooooore we need videos like this mooooorrrrre

Ответить
@briosh.mp4
@briosh.mp4 - 26.06.2023 23:40

ty for this amazing video !

Ответить
@lucamanfroi9111
@lucamanfroi9111 - 23.06.2023 04:27

Hey! Great tutorial! I've been trying to get this right for a week now hahaha. I got it to work, but for some reason, it only works without animation, when I add the animator to my script, it seases to work. Maybe it has somethin to do with my idle animation starting as soon as I leave the edge? If you hav any tips, I would apreciate it a lot :D

Ответить
@stormyyay401
@stormyyay401 - 10.06.2023 13:15

Thanks so much for the amazing video, the camera movement for the 2d game im making is so much nicer and easier than what i was planning to do. The smooth transitions are very satisfying and im sure will be a main part of the feel of the game

Ответить
@alperakin2561
@alperakin2561 - 05.06.2023 04:06

Hello, I adapted what you did in the optimized code to my own code, but I'm still experiencing the issue of the second jump being higher when executed consecutively. There could be two reasons for this. The first one is that instead of using the IsGrounded method, I directly used a bool variable. However, I don't think this is the main issue. I believe the main problem is that I used OnCollisionEnter2D instead of OverlapCircle. When I use OverlapCircle instead of OnCollision2D, the player doesn't jump at all. If you'd like to help, I can provide the code below. Thank you in advance.


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class Gamepad_Controller : MonoBehaviour
{
private Rigidbody2D rb2d;
private Animator animator;
public static Gamepad_Controller instance;

private float horizontal;
private float speed = 8f;
private float jumpingPower = 16f;
private bool isFacingRight = true;
private bool IsGrounded = true;
private bool isJumping;
private float cayoteTime = 0.2f;
private float cayoteTimeCounter;
private float jumpBufferTime = 0.2f;
private float jumpBufferCounter;
public int direction;

[SerializeField] GameObject trailRendererObject;
[SerializeField] bool canDash = true;
[SerializeField] bool isDashing;
[SerializeField] float dashPower;
[SerializeField] float dashTime;
[SerializeField] float dashCooldown;
[SerializeField] float dashGravity;
private float normalGravity;
private float waitTime;
private TrailRenderer tr;


private void Awake()
{
animator = GetComponent<Animator>();
instance = this;
rb2d = GetComponent<Rigidbody2D>();
normalGravity = rb2d.gravityScale;
tr = trailRendererObject.GetComponent<TrailRenderer>();
}

// Start is called before the first frame update
void Start()
{
//rb2d = GetComponent<Rigidbody2D>();
}

// Update is called once per frame
void Update()
{
if (isDashing)
{
return;
}

if (IsGrounded)
{
cayoteTimeCounter = cayoteTime;
}
else
{
cayoteTimeCounter -= Time.deltaTime;
}

if (!isFacingRight && horizontal > 0f)
{
Flip();
}
else if (isFacingRight && horizontal < 0f)
{
Flip();
}
}

private void FixedUpdate()
{


if (isDashing)
{
return;
}

rb2d.velocity = new Vector2(horizontal * speed, rb2d.velocity.y);

if (jumpBufferCounter > 0f && cayoteTimeCounter > 0f && !isJumping)
{
rb2d.velocity = new Vector2(rb2d.velocity.x, jumpingPower);
animator.SetTrigger("jump");
animator.SetBool("grounded", false);
IsGrounded = false;
jumpBufferCounter = 0f;
StartCoroutine(JumpCooldown());
}
}

private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Zemin"))
{
IsGrounded = true;
animator.SetBool("grounded", true);
}
}

private void Flip()
{
isFacingRight = !isFacingRight;
Vector3 localScale = transform.localScale;
localScale.x *= -1f;
transform.localScale = localScale;
}

public void Move(InputAction.CallbackContext context)
{
horizontal = context.ReadValue<Vector2>().x;

if (horizontal > 0)
{
direction = 1;

}else if(horizontal < 0)
{
direction = -1;
}
animator.SetFloat("speed", speed);

if (context.canceled)
{
animator.SetFloat("speed", 0f);
}
}

public void Dash(InputAction.CallbackContext context)
{
if(context.performed && canDash)
{
StartCoroutine(Dash());
}
}

public void Jump(InputAction.CallbackContext context)
{
if (context.performed)
{
jumpBufferCounter = jumpBufferTime;
}
else
{
jumpBufferCounter -= Time.deltaTime;
}

if (context.canceled && rb2d.velocity.y > 0f)
{
rb2d.velocity = new Vector2(rb2d.velocity.x, rb2d.velocity.y * 0.5f);
cayoteTimeCounter = 0f;
}
}

IEnumerator Dash()
{
canDash = false;
isDashing = true;
float originalGravity = rb2d.gravityScale;
rb2d.gravityScale = 0;
rb2d.velocity = new Vector2(transform.localScale.x * dashPower, 0);
tr.emitting = true;
yield return new WaitForSeconds(dashTime);
tr.emitting = false;
rb2d.gravityScale = originalGravity;
isDashing = false;
yield return new WaitForSeconds(dashCooldown);
canDash = true;
}

private IEnumerator JumpCooldown()
{
isJumping = true;
yield return new WaitForSeconds(0.4f);
isJumping = false;
}

}

Ответить
@reaksiyon1337
@reaksiyon1337 - 30.05.2023 20:40

Just use raycast for isGrounded to get same result in better way

Ответить
@juanbelmonth1594
@juanbelmonth1594 - 25.05.2023 14:32

the best tutorial, seriously

Ответить
@ushergt9613
@ushergt9613 - 23.05.2023 20:27

Is it called Coyote Time because of the Roadrunner cartoon?

Ответить
@yenndae
@yenndae - 19.05.2023 12:56

better than chatgpt

Ответить
@Kooczsi
@Kooczsi - 09.05.2023 21:30

this is freaking amazing. used it for the character controller brackeys made

Ответить
@lombiolof
@lombiolof - 06.05.2023 07:12

super easy and useful, thanks!

Ответить
@popcorn1561
@popcorn1561 - 05.05.2023 09:51

Whenever I press the jump button right before i land it gives my character a big Jump boost

Ответить
@Folarek
@Folarek - 13.04.2023 22:06

ur a GOAT 🐐🐐🐐

Ответить
@nocultist7050
@nocultist7050 - 31.03.2023 19:37

how to fix coyote time caused double jump

Ответить
@datnamedoh4877
@datnamedoh4877 - 29.03.2023 00:39

Anyone know if there is a tutorial of this using the new input system?

Ответить
@thien837
@thien837 - 17.03.2023 23:27

I got a problem with this jump buffering method but i don’t know how to fix it. So ideally, when you press down on the jump button before the player reaches the ground, they would still jump and you release the button after the jump then the “rb.velocity.y * 0.5f” take into effect. The problem arises when you press and release the jump button before the player reaches the ground. The jump is already registered but because the “rb.velocity.y * 0.5f” is programmed to activate after the jump, it launches the player up with max force. I though reducing the jump buffer time would solve the problem but it only reduces the time that the issue occurs. Please help

Ответить
@d0uglas525
@d0uglas525 - 12.03.2023 19:38

Hey bendux I added double jump to my game but after I added coyote time it stopped working. Why?

Ответить
@emelme
@emelme - 06.03.2023 21:17

If I spam the jump button I can sometimes jump twice. I looked at the values in coyoteTimeCounter, and sometimes instead of when I hit the jump button, coyoteTimeCounter becomes 0, it remains 0.2 and I can jump again. Do you know how to fix it? Thank you in advance

float coyoteTime = 0.2f;
float coyoteTimeCounter;

if (OnGround())
{
coyoteTimeCounter = coyoteTime;
}
else
{
coyoteTimeCounter -= Time.deltaTime;
}

if (Input.GetButtonDown("Jump") && coyoteTimeCounter > 0f)
{
rb.AddForce(jumpForce * Vector2.up, ForceMode2D.Impulse);
coyoteTimeCounter = 0f;
}

Ответить
@affenaffgaffel8330
@affenaffgaffel8330 - 25.02.2023 18:08

Hi I just can't figure out how I use coyote time with the new Input System and it would be really nice if someone could help me.

Ответить
@friendly.dungeonmaster
@friendly.dungeonmaster - 19.02.2023 02:09

Hey very helpful video, but I have a question. I am using your code, like in the description. But my character can jump multiple times while in air.
How do I solve this? I tried so many things. Pls answer in noob language :D

Ответить
@sterling709
@sterling709 - 16.02.2023 18:54

can understand coyote, your video do give good understanding on coyote but not the buffer jump,

Ответить
@michaelshollaj6428
@michaelshollaj6428 - 09.02.2023 15:10

Liked and subscribed quite fast :D pretty amazing video, and I liked the way you explained the code, thanks a lot!!

Ответить
@d0c_dev
@d0c_dev - 02.02.2023 21:27

Thank you mate, it was super easy to implement even on a multiplayer game! amazing work keep it up

Ответить
@aomi5012
@aomi5012 - 22.01.2023 21:46

Hi. Can you do the video with Double Jump, Coyote Time and Jump Buffering. Your video is great but I can't seen to implement Coyo/Buffer to my double jump script.

Ответить
@nightfox_69
@nightfox_69 - 19.01.2023 23:34

Again, GOLD tier tutorial and nice game design tips! Thanksss!!

Ответить
@shadowboi1812
@shadowboi1812 - 15.01.2023 23:22

what I like about the tutorial is how you can implement it onto pretty much any movement script, nice tutorial

Ответить
@doodoo3702
@doodoo3702 - 08.01.2023 17:59

Hey, how do i implement this is new input system? I actually tried to do this myself, but it works ... Wrong. Can you help?

Ответить
@matheuspena475
@matheuspena475 - 07.01.2023 22:28

I'm having a problem, the coyote time makes my character jump significanly lower during the coyotetime, and the timebuffer doesn't work and make the player doublejump

Ответить
@doodoo3702
@doodoo3702 - 06.01.2023 16:44

You are amazing!!

Ответить
@risingforce9648
@risingforce9648 - 27.12.2022 03:03

how can I do this using properties ?

Ответить
@ds1jmadx5
@ds1jmadx5 - 12.12.2022 23:55

Really good tutorial, but how can i get this to work with double jumping? this seems to break my double jump

Ответить