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);
    }
}

在淡入淡出引用闪烁脚本

运行,触发物体就会闪烁

相关推荐
风酥糖9 小时前
Godot游戏练习01-第24节-多人游戏暂停菜单,游戏优化
游戏·游戏引擎·godot
Allen747415 小时前
ComfyUI 自动化生产 3D资产 工作流笔记
图像处理·opencv·unity·自然语言处理·3d模型生成·confyui
nnsix18 小时前
Unity Windows11 打字中文显示不出来输入法的候选框
unity
SCLchuck1 天前
Godot 4 2D 物理引擎位置初始化踩坑:add_child() 和 position 到底谁先? (错误位置触发物理事件)
游戏引擎·godot·游戏开发·物理引擎
adogai1 天前
unity mcp接入 实现一句话生成游戏!
游戏·unity·游戏引擎
mxwin1 天前
Unity Shader 逐像素光照 vs 逐顶点光照性能与画质的权衡策略
unity·游戏引擎·shader·着色器
CDN3601 天前
游戏盾导致 Unity/UE 引擎崩溃的主要原因排查?
游戏·unity·游戏引擎
mxwin1 天前
Unity URP 全局光照 (GI) 完全指南 Lightmap 采样与实时 GI(光照探针、反射探针)的 Shader 集成
unity·游戏引擎·shader·着色器
mxwin1 天前
Unity URP 溶解效果基于噪声纹理与 clip 函数实现物体渐隐渐显
unity·游戏引擎·shader
CheerWWW1 天前
GameFramework——Download篇
笔记·学习·unity·c#