首页 > 解决方案 > 统一粒子系统问题

问题描述

我正在制作一款赛车游戏,并且希望在您越野时有岩石从轮胎上飞出。我遇到的问题是粒子系统没有在应该触发的时候触发。我用它作为地面来触发一个 bool 并且 bool 可以很好地触发。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GroundEffect : MonoBehaviour
{
 public void OnTriggerEnter(Collider other)
 {
    if (other.gameObject.CompareTag("Player"))
    {
        other.transform.root.GetComponentInChildren<KartController>().isOnGround = true;
    }
 }

 public void OnTriggerExit(Collider other)
 {
    if (other.gameObject.CompareTag("Player"))
    {
        other.transform.root.GetComponentInChildren<KartController>().isOnGround = false;
    }
 }
}

奇怪的是,调试日志会触发,但粒子系统不会。我试过把它全部放在地面脚本下,我把它放在“!isOnBoostStrip”下的“isOnGround”下,即使加速交换工作粒子我也没有试过没有“p.Stop( );" 只是让它不循环,仍然没有运气,尽管当我这样做时它做了一件奇怪的事情,它会开火,但只有当我碰到障碍物时才会开火。

非常感谢任何帮助,我不确定我哪里出错了。

编辑:在这个视频中,我展示了我当前的岩石系统和默认粒子系统的平面设置和错误。它还只检查“IsOnGround”是否为真,这似乎工作得很好。这是视频 - https://www.youtube.com/watch?v=P6TfacNZZxE 这是我在视频中使用的代码。

    void Update()
{

    //Follow Collider
    transform.position = sphere.transform.position - new Vector3(0, 0.4f, 0);

    if (!isOnBoostStrip)
    {
        //Accelerate
        if (Input.GetButton("Fire1"))
        {
            speed = curAccel;
        }

        //Reverse
        if (Input.GetButton("Fire2"))
        {
            speed = -curAccel / 3;
        }

        if (!isOnGround)
        {
            curAccel = zAcceleration;
        }
        if (isOnGround)
        {
            curAccel = zAcceleration / 2;
        }
    }
    else if (isOnBoostStrip)
    {
        speed = zAcceleration * boostStripSpeed;
        foreach (ParticleSystem p in exhaustParticles)
        {
            if (!isDrifting)
            {
                c = turboColors[0];
            }
            var pmain = p.main;
            pmain.startColor = c;
            p.Play();
        }
    }

    //Rocks
    if (isOnGround)
    {
        foreach (ParticleSystem p in groundParticles)
        {
            Debug.Log(isOnGround);
            p.Play();
        }
    }

}

标签: unity3dparticle-system

解决方案


Unity 的粒子系统有时可能有点不稳定,我相信您现在已经发现了。我过去使用的一个解决方案是将麻烦的粒子系统设置为Play on Awake. 然后不使用粒子上的Playand ,而是使用Stop粒子的GameObject,SetActive分别使用true和false。


推荐阅读