How can I make it so that the longer you hold down the jump button the higher the player jumps?
So I want the jumping in my game to be similar to games like Mario where if you tap the jump button the character does a small jump but if held down the character does a larger jump. How can I achieve this?
The code for my player movement currently:
public float speedForce = 50f;
public float jumpPower = 150f;
public bool grounded;
public bool canDoubleJump;
private Rigidbody2D rb2d;
private Animator anim;
void Start ()
{
rb2d = gameObject.GetComponent<Rigidbody2D> ();
anim = gameObject.GetComponent<Animator> ();
}
void Update ()
{
anim.SetBool ("Grounded", grounded);
anim.SetFloat ("Speed", Mathf.Abs (rb2d.velocity.x));
//Moving Left/Right
if (Input.GetKey(KeyCode.RightArrow))
{
rb2d.velocity = new Vector2(speedForce, rb2d.velocity.y);
transform.localScale = new Vector3(1, 1, 1);
}
else if (Input.GetKey(KeyCode.LeftArrow))
{
rb2d.velocity = new Vector2(-speedForce, rb2d.velocity.y);
transform.localScale = new Vector3(-1, 1, 1);
}
else
rb2d.velocity = new Vector2(0, rb2d.velocity.y);
//Jumping
if (Input.GetKeyDown (KeyCode.UpArrow))
{
if (grounded == true)
{
rb2d.AddForce(Vector2.up * jumpPower * 2f);
canDoubleJump = true;
}
else
{
if(canDoubleJump == true)
{
canDoubleJump = false;
rb2d.velocity = new Vector2(rb2d.velocity.x, 0);
rb2d.AddForce(Vector2.up * jumpPower * 2f);
}
}
}
}
Comment