一、創建粒子特效游戲物體
二、修改粒子系統屬性
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. 最終效果如下圖所示:
?
? ? ? ? 本章完。感謝閱讀!