(請原諒任何格式問題)我正在嘗試獲得一個簡單的粒子系統來使用 OnTriggerEnter 并停止使用 OnTriggerExit。遵循粒子系統上的 Unity API ( https://docs.unity3d.com/ScriptReference/ParticleSystem.Play.html )。我開發了以下代碼:using System.Collections;using System.Collections.Generic;using UnityEngine;public class Electric_Trap_Trigger : MonoBehaviour{ParticleSystem system{ get { if (_CachedSystem == null) _CachedSystem = transform.GetChild(0).gameObject.GetComponent<ParticleSystem>(); return _CachedSystem; }}private ParticleSystem _CachedSystem;public bool includeChildren = true;//Start is called before the first frame updatevoid Start(){ if (system != null) Debug.Log("Trap found");}// Update is called once per framevoid Update(){}private void OnTriggerEnter(Collider other){ if(other.gameObject.tag == "Player") { Debug.Log("Trap Triggered by: " + other.gameObject.tag); if(system != null) { system.Play(includeChildren); } }}private void OnTriggerExit(Collider other){ if (other.gameObject.tag == "Player") { Debug.Log("Trap Exited by: " + other.gameObject.tag); if (system != null) { system.Stop(includeChildren); } }}}正如你所看到的,我有調試代碼報告粒子系統已經找到并且玩家對象確實與盒子碰撞器交互。粒子系統不播放。任何幫助將不勝感激。
1 回答

慕妹3242003
TA貢獻1824條經驗 獲得超6個贊
如建議:
我的特殊問題的答案:“如何讓現有的 Unity ParticleSystem 通過腳本播放”(更具描述性的標題)。已解決。請注意,需要進一步研究才能了解答案為何有效。
ParticleSystem 中的一種設置稱為 Prewarm。啟用該設置允許 system.Play 和 system.Stop 代碼啟動和停止粒子系統。如前所述,我還不知道為什么將該設置更改為 true(啟用)允許代碼工作。
沒有進行適當的研究。我只是在玩設置,一次一個。
- 1 回答
- 0 關注
- 168 瀏覽
添加回答
舉報
0/150
提交
取消