Advertisement

How can i combine Coyote Time and Jump Buffering with double jump?

Started by November 26, 2021 11:53 PM
4 comments, last by phlp 2 years, 11 months ago

Hello,

I am working on a Character Controller, for this i followed a video... just like in the video I also included double Jump (https://www.youtube.com/watch?v=QGDeafTx5ug&ab_channel=Blackthornprod)

But now i wanted to also implement Coyote Time and Jump Buffering... so how can i do this ?

I followed some Tutorials for them but they all do not with the double jump mechanic...

I dont know much about programming so I really need help..

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class CharacterController : MonoBehaviour

{

public float speed;

public float jumpForce;

private float moveInput;

private bool facingRight = true;

private Rigidbody2D rb;

private bool isGrounded;

public Transform groundCheck;

public float checkRadius;

public LayerMask whatIsGround;

private int extraJumps;

public int extraJumpsValue;

void Start()

{

extraJumps = extraJumpsValue;

rb = GetComponent<Rigidbody2D>();

}

void FixedUpdate()

{

isGrounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, whatIsGround);

moveInput = Input.GetAxis("Horizontal");

rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);

if(facingRight == false && moveInput > 0)

{

Flip();

}

else if(facingRight == true && moveInput < 0)

{

Flip();

}

}

void Update()

{

if(isGrounded == true)

{

extraJumps = extraJumpsValue;

}

if(Input.GetKeyDown(KeyCode.W) && extraJumps > 0)

{

rb.velocity = Vector2.up * jumpForce;

extraJumps--;

}

else if(Input.GetKeyDown(KeyCode.W) && extraJumps == 0 && isGrounded == true)

{

rb.velocity = Vector2.up * jumpForce;

}

}

void Flip()

{

facingRight = !facingRight;

Vector3 Scaler = transform.localScale;

Scaler.x *= -1;

transform.localScale = Scaler;

}

}

I really hope you Guys can Help me... thanks for every Answer :)

How about something like this:

int jumps = 0; // Track jumps made so far
int jumpsAvailable = 2; // Double jump
float coyoteTime = 0.1f; // It's in the name
float coyoteStart; // When we last touched ground

// You should probably compartmentalize this
void Update()
{
	//Do your ground check logic here. I would think Raycast is better than a circle, because a circle will return if ground is to either side of you or if you're moving up through a platform. Either way, now we have a bool isGrounded.
	if (isGrounded)
	{
		coyoteStart = Time.time;
	}
	else
	{
		if (Time.time > (coyoteStart + coyoteTime)
		{
			if (jumps = 0) jumps = 1; //If player hasn't jumped yet, discard their first jump
		}
	}
	
	if (jump input)
	{
		if (jumps < jumpsAvailable)
		{
			// Jump logic
			jumps += 1;
		}
	}
}

It's sloppy, but hopefully you get the idea.

Basically, have a reservoir of jumps. If the player jumps, deduct one. When coyote time runs out, if the player hasn't jumped yet, deduct the first jump.

My itch.io page, just game jams so far

Advertisement

Sorry, I completely glossed over the jump buffering bit. For that you could use something like

float jumpBuffer = 0.1f;
float lastInput;
bool jump = False;

void Update()
{
	if (Input.keyDown) 
	{
		lastInput = Time.time;
	}
	if (isGrounded)
	{
		if (Time.time <= (lastInput + jumpBuffer))
		{
			jump = true;
		}
	}
}

Then, in the previous script, jump input would just be your bool jump

I haven't tried any of this; I haven't even made a platformer yet. But I think the idea should work.

My itch.io page, just game jams so far

@phlp thank you… im going to try this later…

but really thanks for the answer ?

@Peee3 No problem. Let me know how it works out.

My itch.io page, just game jams so far

This topic is closed to new replies.

Advertisement