using UnityEngine; using System.Collections; using DG.Tweening; using UnityEngine.SceneManagement; public class BirdControl : MonoBehaviour { public int rotateRate = 10; public float upSpeed = 10; public GameObject scoreMgr; public AudioClip jumpUp; public AudioClip hit; public AudioClip score; public bool inGame = false; private bool dead = false; private bool landed = false; private Sequence birdSequence; // Use this for initialization void Start () { float birdOffset = 0.05f; float birdTime = 0.3f; float birdStartY = transform.position.y; birdSequence = DOTween.Sequence(); birdSequence.Append(transform.DOMoveY(birdStartY + birdOffset, birdTime).SetEase(Ease.Linear)) .Append(transform.DOMoveY(birdStartY - 2 * birdOffset, 2 * birdTime).SetEase(Ease.Linear)) .Append(transform.DOMoveY(birdStartY, birdTime).SetEase(Ease.Linear)) .SetLoops(-1); } // Update is called once per frame void Update () { if (!inGame) { return; } birdSequence.Kill(); if (!dead) { if (Input.GetButtonDown("Fire1")) { JumpUp(); } } if (!landed) { float v = transform.GetComponent().velocity.y; float rotate = Mathf.Min(Mathf.Max(-90, v * rotateRate + 60), 30); transform.rotation = Quaternion.Euler(0f, 0f, rotate); } else { transform.GetComponent().rotation = -90; } } void OnTriggerEnter2D (Collider2D other) { if (other.name == "land" || other.name == "pipe_up" || other.name == "pipe_down") { if (!dead) { GameObject[] objs = GameObject.FindGameObjectsWithTag("movable"); foreach (GameObject g in objs) { g.BroadcastMessage("GameOver"); } GetComponent().SetTrigger("die"); AudioSource.PlayClipAtPoint(hit, Vector3.zero); } if (other.name == "land") { transform.GetComponent().gravityScale = 0; transform.GetComponent().velocity = new Vector2(0, 0); landed = true; } } if (other.name == "pass_trigger") { scoreMgr.GetComponent().AddScore(); AudioSource.PlayClipAtPoint(score, Vector3.zero); } } public void JumpUp() { transform.GetComponent().velocity = new Vector2(0, upSpeed); AudioSource.PlayClipAtPoint(jumpUp, Vector3.zero); } public void GameOver() { dead = true; SceneManager.LoadScene(0); } }