在 Unity 中启动粒子系统

(请原谅任何格式问题)我正在尝试获得一个简单的粒子系统来使用 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 update

void Start()

{

    if (system != null)

        Debug.Log("Trap found");

}


// Update is called once per frame

void 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);

        }

    }

}


}

正如你所看到的,我有调试代码报告粒子系统已经找到并且玩家对象确实与盒子碰撞器交互。粒子系统不播放。任何帮助将不胜感激。


蝴蝶不菲
浏览 244回答 1
1回答

慕妹3242003

如建议:我的特殊问题的答案:“如何让现有的 Unity ParticleSystem 通过脚本播放”(更具描述性的标题)。已解决。请注意,需要进一步研究才能了解答案为何有效。ParticleSystem 中的一种设置称为 Prewarm。启用该设置允许 system.Play 和 system.Stop 代码启动和停止粒子系统。如前所述,我还不知道为什么将该设置更改为 true(启用)允许代码工作。没有进行适当的研究。我只是在玩设置,一次一个。
打开App,查看更多内容
随时随地看视频慕课网APP