I have tried to make a double jumping feature in my game. It works fine. But. A player can jump after leaving a platform if a jump is done. If he uses double jump before he lands, he can’t jump after leaving a platform. In my game I don’t want a Player to be able to jump if he falls off a platform.
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public float moveSpeed;
public float jumpForce;
public bool grounded;
public LayerMask whatIsGround;
private Rigidbody2D rb;
private Collider2D coll;
private Animator anim;
private bool canDoubleJump;
void Start ()
{
rb = GetComponent<Rigidbody2D> ();
coll = GetComponent<Collider2D> ();
anim = GetComponent<Animator> ();
}
void FixedUpdate ()
{
grounded = Physics2D.IsTouchingLayers (coll, whatIsGround);
}
void Update ()
{
rb.velocity = new Vector2 (moveSpeed, rb.velocity.y);
if (Input.GetButtonDown ("Fire1"))
{
if (grounded)
{
rb.velocity = new Vector2 (rb.velocity.x, jumpForce);
canDoubleJump = true;
}
if (!grounded && canDoubleJump)
{
rb.velocity = new Vector2 (rb.velocity.x, jumpForce);
canDoubleJump = false;
}
}
anim.SetFloat ("Speed", rb.velocity.x);
anim.SetBool ("Grounded", grounded);
}
}
My guess is to somehow reset a “canDoubleJump” after a Player lands but everything I tried didn’t work. So. I need some help. I am a begginer in scripting and Unity as you can obviously see. And my English is not that well)
Thanks!