《杜增强讲Unity之Tanks坦克大战》8-子弹碰撞处理


8 子弹碰撞处理

为了处理子弹打到坦克的伤害我们在这里新建一个Shell.cs

子弹有两种情况,碰到坦克炸开,没有碰到坦克则过2s子弹销毁.

void Start () {

    Destroy (gameObject, 2); // 过2秒子弹销毁 } 

碰到子弹我们这里使用OnTriggerEnter,要想这个发生首先要确保Shell预设体里面Collider的Is Trigger已经被选中了.

 
image

然后在OnTriggerEnter里面我们检测在一定范围之内的坦克有哪些.

private float radius = 5f; // 爆炸范围

public LayerMask mask; 

我们需要指定Tank属于哪个LayerMask

 
image

将Shell.cs挂载到Shell预设体上,设置相同的LayerMask

 
image

然后我们使用Physics.OverlapSphere就可以找到以子弹为中心,radius范围之内的所有坦克

Collider[] colliders = Physics.OverlapSphere( transform.position, radius, mask);

然后遍历所有坦克,根据两者之间的距离和最大伤害值100计算坦克所受伤害

Health health = colliders [i].GetComponent<Health> (); // 找到Health组件

        float damage = Vector3.Distance (transform.position, colliders [i].transform.position) / 5 * 100; // 根据实际距离按比例计算伤害值 if (health) health.TakeDamage (damage); // 坦克承受伤害 

另外一个就是为坦克添加被炸开的效果

Rigidbody rb = colliders [i].GetComponent<Rigidbody> (); // 刚体组件

        if( rb ) rb.AddExplosionForce (1000f, transform.position, radius); // 坦克被炸开 

最后是子弹炸毁效果和音效

private ParticleSystem ps; // 爆炸效果

private AudioSource audioSource; // 声源 

在Start里面获取ps和audioSource

ps = GetComponentInChildren <ParticleSystem> ();

    audioSource = GetComponent<AudioSource> (); 

然后在OnTriggerEnter最后播放爆炸效果,爆炸运行,销毁gameObject

ps.transform.parent = null; // 将爆炸效果从Shell里面移出

    ps.Play (); // 播放爆炸效果 audioSource.Play (); // 播放爆炸音效 Destroy (ps.gameObject, ps.duration); // 爆炸效果播放完毕之后移出爆炸效果的gameObject Destroy (gameObject); // 移出Shell的gameObject 

最终版本代码为:Shell.cs

 
image
 

using UnityEngine;

using System.Collections;

public class Shell : MonoBehaviour {

private float radius = 5f; // 爆炸范围

public LayerMask mask; // tank

private ParticleSystem ps; // 爆炸效果

private AudioSource audioSource; // 声源 // Use this for initialization void Start () { Destroy (gameObject, 2); // 过2秒子弹销毁 ps = GetComponentInChildren <ParticleSystem> (); // 获取子对象的ParticleSystem audioSource = GetComponent<AudioSource> (); // 获取音源 } // Update is called once per frame void OnTriggerEnter ( Collider other) { Collider[] colliders = Physics.OverlapSphere( transform.position, radius, mask); // radius范围内所有坦克 for (int i = 0; i < colliders.Length; i++) { // 遍历所有坦克 Health health = colliders [i].GetComponent<Health> (); // 找到Health组件 float damage = Vector3.Distance (transform.position, colliders [i].transform.position) / 5 * 100; // 根据实际距离按比例计算伤害值 if (health) health.TakeDamage (damage); // 坦克承受伤害 Rigidbody rb = colliders [i].GetComponent<Rigidbody> (); // 刚体组件 if( rb ) rb.AddExplosionForce (1000f, transform.position, radius); // 坦克被炸开 } ps.transform.parent = null; // 将爆炸效果从Shell里面移出 ps.Play (); // 播放爆炸效果 audioSource.Play (); // 播放爆炸音效 Destroy (ps.gameObject, ps.duration); // 爆炸效果播放完毕之后移出爆炸效果的gameObject Destroy (gameObject); // 移出Shell的gameObject } 

}

 

     

---------------------------我是目录分割线---------------------------

《杜增强讲Unity之Tanks坦克大战》1-准备工作

《杜增强讲Unity之Tanks坦克大战》2-场景设置

《杜增强讲Unity之Tanks坦克大战》3-添加坦克

《杜增强讲Unity之Tanks坦克大战》4-坦克的移动和旋转

《杜增强讲Unity之Tanks坦克大战》5-子弹

《杜增强讲Unity之Tanks坦克大战》6-发射子弹

《杜增强讲Unity之Tanks坦克大战》7-坦克血条

《杜增强讲Unity之Tanks坦克大战》8-子弹碰撞处理

《杜增强讲Unity之Tanks坦克大战》9-发射子弹时蓄力

《杜增强讲Unity之Tanks坦克大战》10-相机控制

《杜增强讲Unity之Tanks坦克大战》11-游戏流程控制

---------------------------我是目录分割线---------------------------


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM