flappy_bird/Assets/Scripts/BirdScript.cs
2025-01-26 18:07:27 +05:30

40 lines
1018 B
C#

using UnityEngine;
public class BirdScript : MonoBehaviour
{
public Rigidbody2D myRigidbody;
public float flapStrength;
public LogicScript logic;
public bool birdIsAlive = true;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
logic = GameObject.FindGameObjectWithTag("Logic").GetComponent<LogicScript>();
}
// Update is called once per frame
public void jump()
{
myRigidbody.linearVelocity = Vector2.up * flapStrength;
}
void Update()
{
if(Input.GetKeyDown(KeyCode.Space) && birdIsAlive)
{
myRigidbody.linearVelocity = Vector2.up * flapStrength;
}
if(transform.position.y > 16 || transform.position.y < -16)
{
logic.gameOver();
birdIsAlive = false;
}
}
private void OnCollisionEnter2D(Collision2D other)
{
logic.gameOver();
birdIsAlive = false;
}
}