在Unity中启动粒子系统

时间:2019-03-07 16:17:19

标签: c# unity3d

(请原谅任何格式问题) 我试图获得一个简单的粒子系统来与OnTriggerEnter一起玩,并以OnTriggerExit停止。遵循粒子系统(https://docs.unity3d.com/ScriptReference/ParticleSystem.Play.html)上的Unity API。我开发了以下代码:

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

}

如您所见,我有调试代码报告已找到粒子系统,并且播放器对象确实与盒子对撞机交互。粒子系统无法播放。任何帮助将不胜感激。

已回答的答案: playing particle system in Unity How to start and stop a particle system in Unity ? Properly play Particule System component? How to start and stop a particle system in Unity ?

1 个答案:

答案 0 :(得分:2)

根据建议:

我的特殊问题的答案:“如何使现有的Unity ParticleSystem通过脚本播放”(描述性稍强的标题)。已解决。请注意,需要进一步研究才能理解答案的作用。

ParticleSystem中的一个设置称为“预热”。启用该设置将允许system.Play和system.Stop代码启动和停止粒子系统。如前所述,我仍然不知道为什么将该设置更改为true(启用)可以使代码正常工作。

未进行适当的研究。我只是一次玩一次设置。

相关问题