SHARE
TWEET

Untitled

a guest Oct 23rd, 2019 66 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.SceneManagement;
  4. public class BirdScript : MonoBehaviour {
  5.     // this global variable will be set from the inspector. Represents bird jump force
  6.     public Vector2 jumpForce = new Vector2();
  7.     //This is where you'll set the sprites in the editor
  8.     public Sprite fly;
  9.     public Sprite flap;
  10.     private SpriteRenderer spriteRenderer;
  11.     // function to be executed once the bird is created
  12.     void Start () {
  13.         // placing the bird
  14.         transform.position = new Vector2(-2f,0f);
  15.         //tk
  16.         spriteRenderer = GetComponent<SpriteRenderer>();
  17.         if (spriteRenderer.sprite == null){
  18.             spriteRenderer.sprite = fly;
  19.         }
  20.     }
  21.     // function to be executed at each frame
  22.     void Update () {
  23.         // waiting for mouse input
  24.         if (Input.GetButtonDown("Fire1")) {
  25.             // setting bird's rigid body velocity to zero
  26.             GetComponent<Rigidbody2D>().velocity = Vector2.zero;
  27.             // adding jump force to bird's rigid body
  28.             GetComponent<Rigidbody2D>().AddForce(jumpForce);
  29.             AudioSource audio = GetComponent<AudioSource>();
  30.             audio.Play();
  31.             //this triggers the animation every time you left click
  32.             spriteRenderer.sprite = flap;
  33.         }
  34.         //this switches the sprite back when you release the left click
  35.         if (Input.GetButtonUp ("Fire1")){
  36.             spriteRenderer.sprite = fly;
  37.         }
  38.         // getting the real position, in pixels, of the bird on the stage
  39.         Vector2 stagePos = Camera.main.WorldToScreenPoint(transform.position);
  40.         // if the bird leaves the stage...
  41.         if (stagePos.y > Screen.height || stagePos.y < 0){
  42.             // ... call die function
  43.             die();
  44.         }
  45.     }
  46.     // function to be executed once the bird enters in collision with anything
  47.     void OnCollisionEnter2D(){
  48.         // call die function
  49.         die();
  50.     }
  51.     void die(){
  52.         // reload the current scene - actually restart the game
  53.         SceneManager.LoadScene("Lose_Scene");
  54.     }
  55. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top