- Home /
Problem with buttons
My code doesn´t work when i add _fall and _jump to a button script.
Code:
using UnityEngine; using System.Collections;
public class PlayerController : PhysicsObject { public float upForce;
private bool isDead = false; public float speed;
private Animator anim;
private Rigidbody2D rb2d;
void Start()
{
anim = GetComponent<Animator> ();
rb2d = GetComponent<Rigidbody2D>();
}
void Update(){
_jump();
_fall();
}
public void _jump(){
if (isDead == false && grounded)
{
anim.SetTrigger("Jump");
rb2d.velocity = Vector2.zero;
rb2d.AddForce(new Vector2(0, upForce));
}
}
public void _fall(){
if (isDead == false)
{
rb2d.velocity = Vector2.zero;
rb2d.AddForce(new Vector2(0,-speed));
}
}
void OnCollisionEnter2D(Collision2D other)
{
if (other.collider.tag == "Ground") {
rb2d.velocity = Vector2.zero;
isDead = true;
GameControl.instance.BirdDied ();
}
}
}
Answer by psromero6 · Aug 04, 2017 at 02:06 PM
Nothing is happening because you are setting the velocity to zero twice per frame. You should remove those calls and you might get closer to what you are expecting.
For most physics you want to avoid setting the velocity directly. It cancels out all calls to addForce() you made before setting the velocity.
Your answer
Follow this Question
Related Questions
Why isn't my joystick code work ? 0 Answers
change the source image in update function? help? 1 Answer
Button on Panel is not responding 1 Answer
How to create a UIToolkit Flashing button 0 Answers
Add selection in GraphView via code 1 Answer