Advertisement
FourWindBadger

Birdscript

Sep 14th, 2023
537
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.36 KB | Gaming | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using Unity.VisualScripting;
  4. using UnityEngine;
  5.  
  6. public class BirdScript : MonoBehaviour
  7. {
  8.     public AudioSource flap;
  9.     public AudioSource dead;
  10.  
  11.     public LogicScript logic;
  12.     public Rigidbody2D myRididbody;
  13.     public SpriteRenderer wingflap;
  14.  
  15.  
  16.     public float flapStrength;
  17.     public bool BirdIsAlive = true;
  18.     public float flaptimming = 1;
  19.     private float flaptimer = 0;
  20.  
  21.     // Start is called before the first frame update
  22.     void Start()
  23.     {
  24.         logic = GameObject.FindGameObjectWithTag("Logic").GetComponent<LogicScript>();
  25.         //Those two are from another tutorial to check if it works or not
  26.         //dead = gameObject.AddComponent<AudioSource>();
  27.         //flap = gameObject.AddComponent<AudioSource>();
  28.     }
  29.  
  30.     // Update is called once per frame
  31.     void Update()
  32.     {
  33.         //Moving the bird
  34.             if (Input.GetKeyDown(KeyCode.Space) == true && BirdIsAlive)
  35.             {
  36.                 myRididbody.velocity = Vector2.up * flapStrength ;
  37.                 flap.Play(0);
  38.            
  39.             }
  40.         //Changes the wings so it looks like its flapping
  41.         if (Input.GetKeyDown(KeyCode.Space)){
  42.            
  43.  
  44.                 if (flaptimer < flaptimming)
  45.                 {
  46.                     flaptimer = flaptimer + Time.deltaTime;
  47.                     wingflap.flipY = true;              
  48.                 }
  49.                 else
  50.                 {
  51.                     flaptimer = 0;
  52.                     wingflap.flipY = false;
  53.                 }
  54.  
  55.             }
  56.  
  57.         //Detects if you flying too high or low
  58.         if (transform.position.y > 17 || transform.position.y < -16)
  59.         {
  60.             Dying();
  61.             logic.gameOver();
  62.             //Seems like it does die if i fly too high or too low but it dosnt play the death sound
  63.             //It does play the flap sound if i change it down in dying from dead.Play(); to flap.play() but apparently not if i fly
  64.             //too high or too low
  65.            
  66.         }
  67.  
  68.     }
  69.  
  70.     private void OnCollisionEnter2D(Collision2D collision)
  71.     {
  72.         Dying();
  73.     }
  74.     void Dying()
  75.     {
  76.        
  77.         logic.gameOver();
  78.         BirdIsAlive = false;
  79.         Debug.Log("Is it playing death sound?");
  80.         dead.volume = 1.0f;
  81.         dead.Play(0);
  82.  
  83.     }
  84. }
  85.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement