I have a jump command in my Update function that makes the player jump. Right now I’m making the player jump using the space key. But I want it to jump using a UI button. Please advice!
Here’s the code.
Thank You!
private Rigidbody2D rb;
public float power = 200f;
public bool jump;
void Start () {;
rb = GetComponent<Rigidbody2D> ();
jump = true;
}
void Update () {
if (jump) {
if (Input.GetKeyDown (KeyCode.Space)) {
rb.velocity = Vector2.zero;
rb.AddForce (new Vector2 (0, power));
}
}
}
void OnCollisionEnter2D(Collision2D col){
if (col.gameObject.tag == "Ground") {
jump = true;
}
}
void OnCollisionExit2D(Collision2D col2){
if (col2.gameObject.tag == "Ground") {
jump = false;
}
}
}