unity 2d jump
rigidbody2D.AddForce(new Vector2(0, 10), ForceMode2D.Impulse);
unity 2d jump
rigidbody2D.AddForce(new Vector2(0, 10), ForceMode2D.Impulse);
double jump script unity
There are a lot of ways to implement the "double jump" feature. The most straight forward would be this one:
public int maxJumps = 2;
private int jumps;
private float jumpForce = 5f;
void Update()
{
if(Input.GetKeyDown(KeyCode.Space) || Input.GetKeyDown(KeyCode.W))
{
this.Jump ();
}
}
private void Jump()
{
if (jumps > 0)
{
gameObject.GetComponent<Rigidbody2D> ().AddForce (new Vector2 (0, jumpForce), ForcMode2D.Impulse);
grounded = false;
jumps = jumps - 1;
}
if (jumps == 0)
{
return;
}
}
void OnCollisionEnter2D(Collision2D collider)
{
if(collider.gameObject.tag == "Ground")
{
jumps = maxJumps;
grounded = true;
movespeed = 2.0F;
}
}
Note that you could have another bool field in order to check if the player has already jumped twice, but using an integer gives you a more flexible solution in case you want to change the game design in the future (ex: letting the user jump three times when using a power-up).
Hope this helps!
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us