SIKI学习1——见缝插针11练习-游戏结束动画的显示

游戏结束的动画,以及游戏结束后加载场景重新开始游戏

using UnityEngine.SceneManagement;//加载场景命名空间引用
public class GameManager : MonoBehaviour
{
    private Transform StartPoint;//针的发射点
    private Transform SpawnPoint;//针的生成点
    public GameObject pinPrefab;//针的预设物,需要拖拽
    private Pin currentPin;//获取当前针,以方便调用方法
    private bool isGameOver = false;//判定游戏是否结束
    private int score = 0;
    public Text scoreText;//控制分数的显示
    private Camera mainCamera;
    public float speed = 3;//动画的速度
    void Start ()
    {
        StartPoint = GameObject.Find("StartPoint").transform;
        SpawnPoint = GameObject.Find("SpawnPoint").transform;
        mainCamera = Camera.main;
        SpawnPin();
    }
    private void Update()
    {
        if (isGameOver) return;//控制针的生成和发射
        if (Input.GetMouseButtonDown(0))
        {
            score++;
            scoreText.text = score.ToString();
            currentPin.StartFly();//调用飞行的方法
            SpawnPin();
        }
    }
    void SpawnPin()
    {
        currentPin= GameObject.Instantiate(pinPrefab,SpawnPoint.position,pinPrefab.transform.rotation).GetComponent<Pin>();
    }
    public void GameOver()
    {
        if (isGameOver) return;//保证这个方法只执行一次
        GameObject.Find("Circle").GetComponent<RotateSelf>().enabled = false;
        StartCoroutine(GameOverAnimation());
        isGameOver = true;
    }
    //利用协程控制动画
    IEnumerator GameOverAnimation()
    {
        while (true)
        {
            mainCamera.backgroundColor = Color.Lerp(mainCamera.backgroundColor, Color.red, speed * Time.deltaTime);//差值进行改变
            mainCamera.orthographicSize = Mathf.Lerp(mainCamera.orthographicSize, 4, speed * Time.deltaTime);
            if (Mathf.Abs(mainCamera.orthographicSize-4)<0.01f)
            {
                break;                                                                                                                                                                                                                                                                                                                    
            }
            yield return 0;
        }
        yield return new WaitForSeconds(0.2f);//等待0.2秒重新加载
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);//游戏结束后,重新加载当前场景
    }
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值