一、创建粒子特效游戏物体
二、修改粒子系统属性
1. 基础属性
(1)修改发射粒子持续时间(Duration)为1s
(2)取消勾选循环(Looping)
(2)修改粒子存在时间(Start Lifetime)为0.1~0.2之间的随机值
(3)修改粒子初始速度(Start Speed)为0
(4)修改粒子初始尺寸(Start Size)为0.7~1之间的随机值
(5)修改粒子初始旋转角度(Start Rotation)为0~360之间的随机值
2. 发射器(Emission)
(1)设置发射粒子速度(Rate over Time)为0(即不发射粒子)
(2)添加爆发(Burst),Time为0,Count为5
3. 发射器形状(Shape)
(1)设置形状(Shape)为Circle
(2)设置旋转角度(Rotation)在x轴方向为900
(3)设置半径为(Radius)0.12
(4)设置模式(Mode)为Burst Spread(爆炸扩散)
4. 粒子颜色(Color over Lifetime)
(1)设置透明度为255-0(即越来越透明)
5. 粒子尺寸(Size over Lifetime)
(1)设置粒子尺寸为0.6-1
6. 粒子纹理动画(Texture Sheet Animation)
(1)添加粒子精灵图片
三、创建命中特效预制体
四、子弹命中敌人时播放特效
1. 编辑子弹脚本:
(1)创建命中特效预制体
public class Bullet : MonoBehaviour
{// 创建命中特效预制体public GameObject hitEffectParticlePrefab;
}
(2)重写迭代器接口,延迟1s后删除命中粒子特效
public class Bullet : MonoBehaviour
{// 创建命中特效预制体public GameObject hitEffectParticlePrefab;// 重写迭代器接口,延迟1s后删除命中粒子特效private IEnumerator deleteHitEffectParticle(GameObject hitEffectParticle, float delay){yield return new WaitForSeconds(delay);Destroy(hitEffectParticle);}
}
(3)当命中敌人时,在子弹的位置创建一个命中粒子特效,在1s后删除
public class Bullet : MonoBehaviour
{// 创建命中特效预制体public GameObject hitEffectParticlePrefab;// 击中敌人private void OnCollisionEnter2D(Collision2D collision){Enemy enemy = collision.gameObject.GetComponent<Enemy>();if(enemy != null) {enemy.changeHealthPoint(-25);GameObject hitEffectParticle = Instantiate(hitEffectParticlePrefab, transform.position, Quaternion.identity);deleteHitEffectParticle(hitEffectParticle, 1);}Destroy(gameObject);}// 重写迭代器接口,延迟1s后删除命中粒子特效private IEnumerator deleteHitEffectParticle(GameObject hitEffectParticle, float delay){yield return new WaitForSeconds(delay);Destroy(hitEffectParticle);}
}
2. 为子弹预制体添加命中粒子特效
3. 最终效果如下图所示:
本章完。感谢阅读!