I would like my object to jump higher when I hold the button down. It can’t be a double jump, just a bigger jump power.
How to do it?
I tried to use bool to mark when the button is pressed, but it doesn’t make much sense.
I guess I should use Time, but I don’t know what to do here.
Can anyone help?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerBehaviour : MonoBehaviour
{
public float moveSpeed;
public float jumpPower = 1;
private Rigidbody2D rb;
public bool isGrounded = true;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
rb.velocity = new Vector2 (moveSpeed, rb.velocity.y);
if(Input.GetMouseButtonDown(0) && isGrounded)
{
//Jump
rb.velocity = Vector2.up * jumpPower;
isGrounded = false;
}
}
void OnCollisionStay2D(Collision2D other)
{
if (other.collider.tag == "Ground")
{
isGrounded = true;
}
}
void OnCollisionExit2D(Collision2D other)
{
if (other.collider.tag == "Ground")
{
isGrounded = false;
}
}
}