博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Unity3d 札记-Tanks Tutorial 知识点汇总---子弹爆炸效果的实现
阅读量:6787 次
发布时间:2019-06-26

本文共 4677 字,大约阅读时间需要 15 分钟。

子弹爆炸  是一个范围性的爆炸,整个过程有四部分

1.在该范围内的坦克都会受到冲击力(减血、位移)

2.而且会播放一个爆炸的粒子效果

3.播放音效

4.子弹消失

 

 

1\UI模型搭建

一个SHELL模型 + ShellExplosion爆炸粒子系统 

 

其中Shell 中 应该有

          一个Collider用于触发物理碰撞           -----------胶囊型封装器 Capsule Collider

          一个RigidBody使其具有物理属性,比如受力       

          当然还有一个ShellExplosion粒子系统  

          及相应控制爆炸过程的 script 

 

思考一下 ShellExplosion 爆炸的时候需要什么

             1.物理特效 --------- PaticalSystem  ExplosionParticles

             2.音效    ------------ AudioSource  ExplosionAudio

             3.爆炸半径 ---------- float    ExplosionRadius

             4.爆炸时间 ---------- float maxLifeTime

             5.爆炸产生的冲击力  ------- float ExplosionForce  

             6. 爆炸造成的最大伤害  -----  float maxDamage 

             7.作用对象    --------LayerMask  TankMask

            

再考虑一下 从外部引用还是内部定义获取            ,都是从外部引用           Public

 对UI进行相关的设置之后 (Collider的大小和位置, 添加各种组件 )

直接PO代码

 

using UnityEngine;public class ShellExplosion : MonoBehaviour{    public LayerMask m_TankMask;    public ParticleSystem m_ExplosionParticles;           public AudioSource m_ExplosionAudio;                  public float m_MaxDamage = 100f;                      public float m_ExplosionForce = 1000f;                public float m_MaxLifeTime = 2f;                      public float m_ExplosionRadius = 5f;                  private void Start()    {        Destroy(gameObject, m_MaxLifeTime);    }    private void OnTriggerEnter (Collider other)    {        // Collect all the colliders in a sphere from the shell's current position to a radius of the explosion radius.        Collider[] colliders = Physics.OverlapSphere (transform.position, m_ExplosionRadius, m_TankMask);        // Go through all the colliders...        for (int i = 0; i < colliders.Length; i++)        {            // ... and find their rigidbody.            Rigidbody targetRigidbody = colliders[i].GetComponent
(); // If they don't have a rigidbody, go on to the next collider. if (!targetRigidbody) continue; // Add an explosion force. targetRigidbody.AddExplosionForce (m_ExplosionForce, transform.position, m_ExplosionRadius); // Find the TankHealth script associated with the rigidbody. TankHealth targetHealth = targetRigidbody.GetComponent
(); // If there is no TankHealth script attached to the gameobject, go on to the next collider. if (!targetHealth) continue; // Calculate the amount of damage the target should take based on it's distance from the shell. float damage = CalculateDamage (targetRigidbody.position); // Deal this damage to the tank. targetHealth.TakeDamage (damage); } // Unparent the particles from the shell. m_ExplosionParticles.transform.parent = null; // Play the particle system. m_ExplosionParticles.Play(); // Play the explosion sound effect. m_ExplosionAudio.Play(); // Once the particles have finished, destroy the gameobject they are on. Destroy (m_ExplosionParticles.gameObject, m_ExplosionParticles.duration); // Destroy the shell. Destroy (gameObject); } private float CalculateDamage (Vector3 targetPosition) { // Create a vector from the shell to the target. Vector3 explosionToTarget = targetPosition - transform.position; // Calculate the distance from the shell to the target. float explosionDistance = explosionToTarget.magnitude; // Calculate the proportion of the maximum distance (the explosionRadius) the target is away. float relativeDistance = (m_ExplosionRadius - explosionDistance) / m_ExplosionRadius; // Calculate damage as this proportion of the maximum possible damage. float damage = relativeDistance * m_MaxDamage; // Make sure that the minimum damage is always 0. damage = Mathf.Max (0f, damage); return damage; }}

 

 

比较值得注意和学习的  

 

Collider[] colliders = Physics.OverlapSphere (transform.position, m_ExplosionRadius, m_TankMask); //这个函数以某一位置,某一半径,以及相关的LayerMask作为区分 ,获取球形区域内物体的Collider
targetRigidbody.AddExplosionForce (m_ExplosionForce, transform.position, m_ExplosionRadius);//这个函数,则是RigidBody自带的,根据FORCE , POSITION , ExplosionRadius对自身施加爆炸冲击力            // Find the TankHealth script associated with the rigidbody.            TankHealth targetHealth = targetRigidbody.GetComponent
(); //泛型获取一个 TankHealth对象 // If there is no TankHealth script attached to the gameobject, go on to the next collider. if (!targetHealth) continue; // Calculate the amount of damage the target should take based on it's distance from the shell. float damage = CalculateDamage (targetRigidbody.position); // Deal this damage to the tank. targetHealth.TakeDamage (damage);

转载于:https://www.cnblogs.com/dongfangliu/p/5801758.html

你可能感兴趣的文章
课堂训练
查看>>
HDU 5464:Clarke and problem
查看>>
Web服务器禁止range请求
查看>>
php编译GD库 JPEG Support
查看>>
【转】着色中的数学和物理原理
查看>>
overflow的使用
查看>>
Position Independent Code (PIC) in shared libraries on x64
查看>>
CNBLOG上几位.NET大牛的博客地址(转)
查看>>
接口继承和实现继承的区别
查看>>
spring 的自建request请求
查看>>
数组的相关知识
查看>>
Python中的logger和handler到底是个什么鬼
查看>>
mysql之 openark-kit online ddl
查看>>
mydumper安装、原理介绍
查看>>
值类型和引用类型的详细讨论
查看>>
《ArcGIS Runtime SDK for Android开发笔记》——(12)、自定义方式加载Bundle格式缓存数据...
查看>>
mysql 查询当天、本周,本月,上一个月的数据
查看>>
构建和管理有效API市场的关键步骤
查看>>
B00003 C++标准库 std::bitset
查看>>
字符串最小表示法(1) 朴素算法
查看>>