- Home /
Question by
SonicDirewolf · Oct 28, 2017 at 04:39 PM ·
uibuttonupdate function
Adding commands from Update function to a UI button.
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;
}
}
}
Comment
Best Answer
Answer by Dragate · Oct 28, 2017 at 06:03 PM
Make your UI Button's onClick function call this:
void Jump(){
if (jump){
rb.velocity = Vector2.zero;
rb.AddForce (new Vector2 (0, power));
}
}
Your answer
Follow this Question
Related Questions
Button on Panel is not responding 1 Answer
RPG Maker style menu with UI buttons that are selected by controller 0 Answers
UI Button Event Trigger Update Selected not working 0 Answers
UI Button Set Image 1 Answer
UI Button ON/Off 3 Answers