Advertisement
Guest User

Untitled

a guest
Dec 2nd, 2022
59,392
2
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.43 KB | None | 2 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.     // Start is called before the first frame update
  13.     void Start()
  14.     {
  15.         logic = GameObject.FindGameObjectWithTag("Logic").GetComponent<LogicScript>();
  16.     }
  17.  
  18.     // Update is called once per frame
  19.     void Update()
  20.     {
  21.         if (Input.GetKeyDown(KeyCode.Space) && birdIsAlive)
  22.         {
  23.             myRigidbody.velocity = Vector2.up * flapStrength;
  24.         }
  25.     }
  26.  
  27.     private void OnCollisionEnter2D(Collision2D collision)
  28.     {
  29.         logic.gameOver();
  30.         birdIsAlive = false;
  31.     }
  32. }
  33.  
  34. using System.Collections;
  35. using System.Collections.Generic;
  36. using UnityEngine;
  37.  
  38. public class PipeMoveScript : MonoBehaviour
  39. {
  40.     public float moveSpeed = 5;
  41.     public float deadZone = -45;
  42.  
  43.     // Start is called before the first frame update
  44.     void Start()
  45.     {
  46.        
  47.     }
  48.  
  49.     // Update is called once per frame
  50.     void Update()
  51.     {
  52.         if (transform.position.x > deadZone)
  53.         {
  54.             Destroy(gameObject);
  55.         }
  56.     }
  57. }
  58.  
  59. using System.Collections;
  60. using System.Collections.Generic;
  61. using UnityEngine;
  62.  
  63. public class PipeSpawnScript : MonoBehaviour
  64. {
  65.     public GameObject pipe;
  66.     public float spawnRate = 2;
  67.     private float timer = 0;
  68.     public float heightOffset = 10;
  69.  
  70.     // Start is called before the first frame update
  71.     void Start()
  72.     {
  73.         spawnPipe();
  74.     }
  75.  
  76.     // Update is called once per frame
  77.     void Update()
  78.     {
  79.         if (timer < spawnRate)
  80.         {
  81.             timer = timer + Time.deltaTime;
  82.         }
  83.         else
  84.         {
  85.             spawnPipe();
  86.             timer = 0;
  87.         }
  88.  
  89.     }
  90.  
  91.     void spawnPipe()
  92.     {
  93.         float lowestPoint = transform.position.y - heightOffset;
  94.         float highestPoint = transform.position.y + heightOffset;
  95.  
  96.         Instantiate(pipe, new Vector3(transform.position.x, Random.Range(lowestPoint, highestPoint), 0), transform.rotation);
  97.     }
  98. }
  99.  
  100. using System.Collections;
  101. using System.Collections.Generic;
  102. using UnityEngine;
  103.  
  104. public class PipeMiddleScript : MonoBehaviour
  105. {
  106.     public LogicScript logic;
  107.  
  108.     // Start is called before the first frame update
  109.     void Start()
  110.     {
  111.         logic = GameObject.FindGameObjectWithTag("Logic").GetComponent<LogicScript>();
  112.     }
  113.  
  114.     // Update is called once per frame
  115.     void Update()
  116.     {
  117.        
  118.     }
  119.  
  120.     private void OnTriggerEnter2D(Collider2D collision)
  121.     {
  122.         if (collision.gameObject.layer == 3)
  123.         {
  124.             logic.addScore(1);
  125.         }
  126.        
  127.     }
  128. }
  129.  
  130. using System.Collections;
  131. using System.Collections.Generic;
  132. using UnityEngine;
  133. using UnityEngine.UI;
  134. using UnityEngine.SceneManagement;
  135.  
  136. public class LogicScript : MonoBehaviour
  137. {
  138.     public int playerScore;
  139.     public Text scoreText;
  140.     public GameObject gameOverScreen;
  141.  
  142.     [ContextMenu("Increase Score")]
  143.     public void addScore(int scoreToAdd)
  144.     {
  145.         playerScore = playerScore + scoreToAdd;
  146.         scoreText.text = playerScore.ToString();
  147.     }
  148.  
  149.     public void restartGame()
  150.     {
  151.         SceneManager.LoadScene(SceneManager.GetActiveScene().name);
  152.     }
  153.  
  154.     public void gameOver()
  155.     {
  156.         gameOverScreen.SetActive(true);
  157.     }
  158. }
  159.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement