unity 2d 入门 飞翔小鸟 死亡 显示GameOver(十四)

1、添加Img

create->ui->img
把图片拖进去
在这里插入图片描述

2、和分数一样、调整位置

在这里插入图片描述

3、修改角色脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Fly : MonoBehaviour
{
   
    //获取小鸟(刚体)
    private Rigidbody2D bird;
    //速度
    public float speed;
    //跳跃
    public float jump;
    //是否存活
    public static bool life = true;
    //获取动画器
    private Animator animator;
    //结束图片
    private GameObject gameOver;
    //结束跳转时间
    private float time;

    // Start is called before the first frame update
    void Start()
    {
   
        bird = GetComponent<Rigidbody2D>();
        animator = GetComponent<Animator>();
        gameOver = GameObject.Find("Canvas/Image");
    }

    // Update is called once per frame
    void Update()
    {
   
        //存活的时候才能运动
        if (life)
        {
   
            bird.velocity = new Vector2(speed, bird.velocity.y);
            //鼠标点击给目标一个纵向速度
            if (Input.GetMouseButtonDown(0))
            {
   
                bird.velocity = new Vector2(bird.velocity.x, jump);
            }
        }
        else {
   
            time += Time.deltaTime;
            if (time>=3) {
   
                FadeInOut.SwitchScene("start");
            }
        }
        //当触碰到死亡的时候出现
        gameOver.SetActive(!life);
    }
    //如果碰撞器撞到了某个物体
    private void OnCollisionEnter2D(Collision2D collision)
    {
   
        if (life==true) {
   
            Bling.blinking();
        }
        life = false;
        animator.SetBool("life", false);
    }
}

在这里插入图片描述

4、开始按钮初始化参数

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class StartBtnLis : MonoBehaviour
{
   
    // Start is called before the first frame update
    void Start()
    {
   
        
    }

    // Update is called once per frame
    void Update()
    {
   
        
    }
    //监听鼠标按下
    private void OnMouseDown()
    {
   
        Debug.Log("测试按下");
        //对象按比例缩小
        transform.localScale = transform.localScale * 0.8f;
    }
    //监听鼠标松开
    private void OnMouseUp()
    {
   
        //对象按比例扩大
        transform.localScale = transform.localScale / 0.8f;
        Fly.life = true;
        Score.score = 0;
        FadeInOut.SwitchScene("game");
    }
}

在这里插入图片描述

修改淡入淡出bug,在淡入前点击开始按钮卡住问题

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class FadeInOut : MonoBehaviour
{
   
    //脚本传进来的图片
    public Texture img;
    //透明度
    public static float alpha = 0;
    //淡出
    public static bool fadeOut = false;
    //淡入
    public static bool fadeIn = false;
    //前端传过来的速度
    public float speed;
    //场景
    private static string scene;

    // Start is called before the first frame update
    void Start()
    {
   
        
    }

    // Update is called once per frame
    void Update()
    {
   
        
    }

    //渲染页面调用的得是这个方法
    private void OnGUI()
    {
   
       // Time.deltaTime 上一帧与当前帧间隔帧数
        if (fadeOut){
   
            alpha += speed * Time.deltaTime;
            if (alpha>1) {
   
                fadeOut = false;
                fadeIn = true;
                //场景切换
                SceneManager.LoadScene(scene);
            }
        }
        if (fadeIn) {
   
            alpha -= speed * Time.deltaTime;
            if (alpha<0) {
   
                fadeIn = false;
            }
        }
        //调整透明度
        GUI.color = new Color(GUI.color.r,GUI.color.g,GUI.color.b,alpha);
       //把场景绘制一张黑色图片
        GUI.DrawTexture(new Rect(0,0,Screen.width,Screen.height),img);
    }

    public static void SwitchScene(string newScene)
    {
   
        if (fadeIn) fadeIn = false;
         fadeOut = true;
        scene = newScene;
    }
}

在这里插入图片描述

运行、碰撞死亡就会显示

最近更新

  1. TCP协议是安全的吗?

    2023-12-11 18:38:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-11 18:38:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-11 18:38:03       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-11 18:38:03       20 阅读

热门阅读

  1. C#随笔 | List.Sort()使用小计

    2023-12-11 18:38:03       34 阅读
  2. Go 反射技术判断结构体字段数据为空

    2023-12-11 18:38:03       42 阅读
  3. 面试经典150题(10-13)

    2023-12-11 18:38:03       35 阅读
  4. Android源码下载流程

    2023-12-11 18:38:03       46 阅读
  5. HBase

    HBase

    2023-12-11 18:38:03      34 阅读
  6. 手撕分布式缓存---互斥锁的优化

    2023-12-11 18:38:03       45 阅读
  7. 【CSP】202203-1_未初始化警告Python实现

    2023-12-11 18:38:03       35 阅读
  8. 【互联网小趣味】常用系统架构介绍扫盲

    2023-12-11 18:38:03       38 阅读
  9. mysql基本语法

    2023-12-11 18:38:03       38 阅读