Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using UnityEngine.SceneManagement;
- public class BirdScript : MonoBehaviour {
- // this global variable will be set from the inspector. Represents bird jump force
- public Vector2 jumpForce = new Vector2();
- //This is where you'll set the sprites in the editor
- public Sprite fly;
- public Sprite flap;
- private SpriteRenderer spriteRenderer;
- // function to be executed once the bird is created
- void Start () {
- // placing the bird
- transform.position = new Vector2(-2f,0f);
- //tk
- spriteRenderer = GetComponent<SpriteRenderer>();
- if (spriteRenderer.sprite == null){
- spriteRenderer.sprite = fly;
- }
- }
- // function to be executed at each frame
- void Update () {
- // waiting for mouse input
- if (Input.GetButtonDown("Fire1")) {
- // setting bird's rigid body velocity to zero
- GetComponent<Rigidbody2D>().velocity = Vector2.zero;
- // adding jump force to bird's rigid body
- GetComponent<Rigidbody2D>().AddForce(jumpForce);
- AudioSource audio = GetComponent<AudioSource>();
- audio.Play();
- //this triggers the animation every time you left click
- spriteRenderer.sprite = flap;
- }
- //this switches the sprite back when you release the left click
- if (Input.GetButtonUp ("Fire1")){
- spriteRenderer.sprite = fly;
- }
- // getting the real position, in pixels, of the bird on the stage
- Vector2 stagePos = Camera.main.WorldToScreenPoint(transform.position);
- // if the bird leaves the stage...
- if (stagePos.y > Screen.height || stagePos.y < 0){
- // ... call die function
- die();
- }
- }
- // function to be executed once the bird enters in collision with anything
- void OnCollisionEnter2D(){
- // call die function
- die();
- }
- void die(){
- // reload the current scene - actually restart the game
- SceneManager.LoadScene("Lose_Scene");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement