Advertisement
johnnygoodguy2000

BirdScript

Mar 17th, 2024
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.11 KB | Gaming | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Birdscript : MonoBehaviour
  6. {
  7.     public Rigidbody2D myRigidBody;
  8.     public float flapStrength;
  9.     public LogicScript logic;
  10.     public bool birdIsAlive = true;
  11.    
  12.  
  13.     // Start is called before the first frame update
  14.     void Start()
  15.     {
  16.         logic = GameObject.FindGameObjectWithTag("Logic").GetComponent<LogicScript>();
  17.     }
  18.  
  19.     // Update is called once per frame
  20.     void Update()
  21.     {
  22.         // Check if the bird goes off-screen
  23.         if (transform.position.y > ScreenUtils.ScreenTop || transform.position.y < ScreenUtils.ScreenBottom)
  24.         {
  25.             // Trigger game over if the bird goes off-screen
  26.             logic.gameOver();
  27.             birdIsAlive = false;
  28.         }
  29.  
  30.         // Bird flap input
  31.         if (Input.GetKeyDown(KeyCode.Space) && birdIsAlive)
  32.         {
  33.             myRigidBody.velocity = Vector2.up * flapStrength;
  34.         }
  35.     }
  36.  
  37.     private void OnCollisionEnter2D(Collision2D collision)
  38.     {
  39.         logic.gameOver();
  40.         birdIsAlive = false;
  41.     }
  42. }
  43.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement