Hello,
I´m trying to rebuild flappy bird just to show a mate how powerful the 2D engine is.
My current problem is, that the x postition gets faster every time. The bird shall fly with an constant speed.
Here is the code:
void InputCheck(){
if(Input.GetKeyDown(KeyCode.Space)){
inputJump = true;
}else{
inputJump = false;
}
}
void Move(){
if(inputJump){
moveDirection.y = jumpPower;
}
moveDirection.x = moveDirection.x + speed;
if(speed < flyspeed){
speed = flyspeed;
}
moveDirection.y -= gravity;
controller.Move(moveDirection * Time.deltaTime);
}
can anybody help me?
Flappy Bird would have been made using some physics most likely. Instead of trying to use .Move, add a Rigidbody2D and BoxCollider2D to your bird Sprite, and in FixedUpdate(), use:
void FixedUpdate()
{
if((canJump) (Input.GetKeyDown(KeyCode.Space)))
{
rigidbody2D.velocity = new Vector2(jumpForce,0);
}
}
You’ll have a float variable to define your force if you want, or just put the number directly in there.
Then you simply have all your level elements contained in an empty parent object, that you translate at a constant speed to the left, the bird stays stationary, it’s the proper way to do an endless level to avoid floating point accuracy issues. (You’ll of course needs to write some code to instantiate new chunks of terrain as it moves, that’s a more tricky part)
And also for the bird dying, it’s simply:
void OnCollisionEnter2D(Collision2D colObj)
{
canJump = false;
//disable level movement
//play death animation
//que death GUI stuff
}
You’ll need to add some BoxCollider2D’s to the prefabs of objects you want killing the bird when touched.