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

一、c#脚本

csharp 复制代码
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;
    }
}

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

csharp 复制代码
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);
    }
}

在淡入淡出引用闪烁脚本

运行,触发物体就会闪烁

相关推荐
九章云极AladdinEdu11 小时前
绿色算力技术栈:AI集群功耗建模与动态调频系统
人工智能·pytorch·深度学习·unity·游戏引擎·transformer·gpu算力
伽蓝_游戏15 小时前
UGUI源码剖析(15):Slider的运行时逻辑与编辑器实现
游戏·ui·unity·性能优化·c#·游戏引擎·.net
lrh30251 天前
Custom SRP - Complex Maps
unity·srp·render pipeline
m0_497214151 天前
unity中通过拖拽,自定义scroll view中子物体顺序
unity·游戏引擎
地狱为王1 天前
在Unity中实现DTLN-AEC处理音频文件的功能
unity·aec·降噪
SmalBox1 天前
【URP】Shader绘制棋盘格对比内置管线
unity·渲染
郝学胜-神的一滴2 天前
基于OpenGL封装摄像机类:视图矩阵与透视矩阵的实现
c++·qt·线性代数·矩阵·游戏引擎·图形渲染
EQ-雪梨蛋花汤2 天前
【Unity笔记】Unity 编辑器扩展:打造一个可切换 Config.assets 的顶部菜单插件
unity·编辑器·游戏引擎
SmalBox3 天前
【URP】UnityHLSL顶点片元语义详解
unity·渲染
在路上看风景3 天前
9. Mono项目与Unity的关系
unity