- Home /
Question by
ninjazippy · Jul 15, 2014 at 06:37 AM ·
jumpjumpingfloatincrease
Check if float is increasing/decreasing
Hello, I am in the process of recreating flappy bird, I am fairly new to C# and Unity. I am stuck at jumping, I want to create a float, which will be the bird's altitude, then I want to check if the float is increasing/decreasing. If it is increasing or decreasing, then I want the state that to a boolean named isFlapping. Sorry, I don't have any code that is worthy yet, therefore can't show any code so far. Let's take this code for an example:
using UnityEngine;
using System.Collections;
public class BirdPhysics : MonoBehaviour {
public float jumpHeight = 8;
public bool isFlapping = false; //Check if bird is jumping
void Start () {
}
void Jump(){
rigidbody2D.AddForce(new Vector2(0f,jumpHeight));
}
void Update () {
if(Input.GetKeyDown("up")){
Jump ();
}
}
}
Comment
Answer by robertbu · Jul 15, 2014 at 06:39 AM
Given that you are using rigidbody2D, you can check the 'y' velocity:
In Update() or FixedUpdate(), you can do:
isFlapping = (rigidbody2D.velocoty.y > 0.0);