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

在淡入淡出引用闪烁脚本

运行,触发物体就会闪烁

相关推荐
孟无岐2 小时前
【Laya】Socket 使用指南
websocket·typescript·游戏引擎·游戏程序·laya
tealcwu5 小时前
【Unity资源】Unity MCP 介绍
unity·游戏引擎
晚霞的不甘8 小时前
Flutter for OpenHarmony 引力弹球游戏开发全解析:从零构建一个交互式物理小游戏
前端·flutter·云原生·前端框架·游戏引擎·harmonyos·骨骼绑定
Thomas_YXQ9 小时前
Unity3D中提升AssetBundle加载速度的详细指南
java·spring boot·spring·unity·性能优化·游戏引擎·游戏开发
Miss_SQ10 小时前
Unity接入AI—Deepseek,数据流式传输
unity·ai
chillxiaohan11 小时前
unity 批量修改场景内字体工具
unity
晚霞的不甘12 小时前
Flutter 方块迷阵游戏开发全解析:构建可扩展的关卡式益智游戏
前端·flutter·游戏·游戏引擎·游戏程序·harmonyos
ellis19701 天前
Unity中ScriptableObject用法整理
unity
玉梅小洋1 天前
Unity Muse 完整使用文档:Sprite+Texture专项
unity·ai·游戏引擎
能源革命1 天前
Three.js、Unity、Cesium对比分析
开发语言·javascript·unity