unity 2d 入门 飞翔小鸟 死亡闪烁特效(十三)

一、c#脚本

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

public class Bling : MonoBehaviour
{
   
    public Texture img;
    public float speed;
    public static bool changeWhite = false;
    private float alpha=0f;
    // Start is called before the first frame update
    void Start()
    {
   
        
    }

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

    private void OnGUI()
    {
   
        if (changeWhite) {
   
            alpha += speed * Time.deltaTime;
            if (alpha>=1) {
   
                changeWhite = false;
            }
        }else{
   
            if (alpha>0) {
   
                alpha -= speed * Time.deltaTime;
            }
        }
        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 blinking()
    {
   
        changeWhite = true;
    }
}

二、在角色脚本触发物体脚本中引用闪烁脚本

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;

    // Start is called before the first frame update
    void Start()
    {
   
        bird = GetComponent<Rigidbody2D>();
        animator = GetComponent<Animator>();
    }

    // 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);
            }
        }
    }
    //如果碰撞器撞到了某个物体
    private void OnCollisionEnter2D(Collision2D collision)
    {
   
        if (life==true) {
   
            Bling.blinking();
        }
        life = false;
        animator.SetBool("life", false);
    }
}

在淡入淡出引用闪烁脚本

在这里插入图片描述

运行,触发物体就会闪烁

最近更新

  1. TCP协议是安全的吗?

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

    2023-12-10 18:22:04       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-10 18:22:04       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-10 18:22:04       20 阅读

热门阅读

  1. 如何选择Docker基础镜像

    2023-12-10 18:22:04       28 阅读
  2. 代码随想录 70. 爬楼梯

    2023-12-10 18:22:04       32 阅读
  3. 【前端设计模式】之责任链模式

    2023-12-10 18:22:04       49 阅读
  4. SAP-PP:超实用的表PP顾问必备

    2023-12-10 18:22:04       32 阅读
  5. 深入了解linux下网卡防火墙selinux

    2023-12-10 18:22:04       39 阅读
  6. 力扣:200. 岛屿数量(Python3)

    2023-12-10 18:22:04       42 阅读