Tanks Unity Tutorial - Phase 4 Tank Health

本文介绍如何在Unity中实现坦克的生命值系统及死亡爆炸效果,包括使用Slider显示血量、粒子系统控制爆炸动画及声音播放等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


题外Tips:

Unity Editor中可以设置是 Pivot模式 或者 Center 模式 

Alt + click 视图里的GameObject,可展开所有子项。


实现效果:


Canvas 渲染模式选择 World Space,Rect 才可以调整,默认是不可以的。


Slider控制血量的显示,删除 Handle Slide Area,去掉Interactable的对勾,就可阻止用户修改血量,而只能显示。



TankExplosion uncheck Play On Awake,初始化的时候不执行 该粒子效果

也可以使用 Object pooling 控制对象的初始化和回收

 m_ExplosionParticles = Instantiate(m_ExplosionPrefab).GetComponent<ParticleSystem>();
        m_ExplosionAudio = m_ExplosionParticles.GetComponent<AudioSource>();

        m_ExplosionParticles.gameObject.SetActive(false);

TankHealth.cs:

using UnityEngine;
using UnityEngine.UI;

public class TankHealth : MonoBehaviour
{
    public float m_StartingHealth = 100f;          
    public Slider m_Slider;                        
    public Image m_FillImage;                      
    public Color m_FullHealthColor = Color.green;  
    public Color m_ZeroHealthColor = Color.red;    
    public GameObject m_ExplosionPrefab;
    
    
    private AudioSource m_ExplosionAudio;          
    private ParticleSystem m_ExplosionParticles;   
    private float m_CurrentHealth;  
    private bool m_Dead;            


    private void Awake()
    {
        m_ExplosionParticles = Instantiate(m_ExplosionPrefab).GetComponent<ParticleSystem>();
        m_ExplosionAudio = m_ExplosionParticles.GetComponent<AudioSource>();

        m_ExplosionParticles.gameObject.SetActive(false);
    }


    private void OnEnable()
    {
        m_CurrentHealth = m_StartingHealth;
        m_Dead = false;

        SetHealthUI();
    }
   

    public void TakeDamage(float amount)
    {
        // Adjust the tank's current health, update the UI based on the new health and check whether or not the tank is dead.
        m_CurrentHealth -= amount;
        SetHealthUI();
        if (m_CurrentHealth <= 0f && !m_Dead){
            OnDeath();
        }

    }

    private void SetHealthUI()
    {
        // Adjust the value and colour of the slider.
        m_Slider.value = m_CurrentHealth;
        m_FillImage.color = Color.Lerp(m_ZeroHealthColor,m_FullHealthColor,m_CurrentHealth / m_StartingHealth);


    }


    private void OnDeath()
    {
        // Play the effects for the death of the tank and deactivate it.
        m_Dead = true;
        m_ExplosionParticles.transform.position = transform.position;
        m_ExplosionParticles.gameObject.SetActive(true);

        m_ExplosionParticles.Play();
        m_ExplosionAudio.Play();

        gameObject.SetActive(false);
    }
}


### Three-Tank Water Tank System Dataset 对于Three-Tank水箱系统的数据分析或仿真,在IT领域内通常会涉及到控制理论、流体力学以及自动化等方面的研究。然而,针对特定的Three-Tank水箱系统数据集并不像图像识别或其他热门研究方向那样广泛存在于公共平台之上。 一些大学和科研机构可能会有自己内部构建的数据集用于教学实验或是项目开发,这些资源往往不会公开发布到互联网上。如果希望获取此类数据集,可以考虑联系专门从事过程控制系统研究的学术单位或者企业合作伙伴寻求合作机会[^1]。 另外,也可以通过模拟软件自行生成所需的数据样本。MATLAB/Simulink是一个非常流行的选择,它提供了丰富的工具箱来帮助研究人员建立复杂的物理模型并收集运行过程中产生的各种参数变化情况作为分析依据。下面是一段简单的Matlab代码片段展示如何创建一个基础版本的三罐液位控制系统: ```matlab % 定义初始条件和时间范围 tspan = [0 10]; % 时间跨度 y0 = [0; 0; 0]; % 初始状态向量 (三个储槽的高度) % 设置ODE求解器选项 options = odeset('RelTol',1e-4,'AbsTol',[1e-4 1e-4 1e-4]); % 调用ode45函数进行数值积分计算 [t,y] = ode45(@(t,y) threeTankSystem(t, y), tspan, y0, options); % 绘制结果图 figure; plot(t,y); xlabel('Time'); ylabel('Height of Tanks'); title('Simulation Results for a Three-Tank Liquid Level Control System'); function dydt = threeTankSystem(~, y) A = 1; % 储槽截面积(m²) g = 9.81; % 重力加速度(m/s²) h1 = y(1); % 第一储槽高度(m) h2 = y(2); % 第二储槽高度(m) h3 = y(3); % 第三储槽高度(m) q_in = 0.01; % 输入流量(L/min) k = sqrt(2*g)*A*sqrt(h1-h2)/60; % 流经阀门的质量流量系数 dh1_dt = (-k + q_in)/(A * 60); % d/dt(h1) dh2_dt = ((q_in-sqrt(2*g)*(h2-h3))/(A * 60); % d/dt(h2) dh3_dt = sqrt(2*g)*(h2-h3)/(A * 60); % d/dt(h3) dydt = [dh1_dt; dh2_dt; dh3_dt]; end ``` 此脚本定义了一个基本的三罐液位动态行为,并利用`ode45()`方法对其进行离散化处理得到随时间演化的高度曲线。这只是一个入门级的例子,实际应用中的建模可能还需要加入更多细节因素如管道阻力损失、泵效率特性等影响要素。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值