Unity 粒子系统问题

时间:2021-04-17 22:50:33

标签: unity3d particle-system

我正在制作一款赛车游戏,希望在您进行越野时有岩石从轮胎上飞出。 我遇到的问题是粒子系统在它应该发射的时候没有发射。 我用它在地面上触发 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();
        }
    }

}

1 个答案:

答案 0 :(得分:0)

Unity 的粒子系统有时可能有点不稳定,我相信您现在已经发现了。我过去使用的一个解决方案是将麻烦的粒子系统设置为#include <algorithm> // next_permutation #include <cstdint> // uint32_t #include <iostream> // i/o #include <vector> // vector using uint = std::uint32_t; using uvec = std::vector<uint>; uint andxor(const uvec& a, const uvec& c) { // Start with all bits set uint result = -1; for (std::size_t i = 0; i < c.size(); ++i) { result &= a[i] ^ c[i]; } return result; } uvec solvePermute(const uvec& a, uvec& b) { // next_permutation expects a pre-sorted input std::sort(b.begin(), b.end()); // Initialize the result with the first permutation of b uvec c = b; uint bestResult = andxor(a, c); // Try all permutations of b to maximize the result of andxor while (std::next_permutation(b.begin(), b.end())) { uint result = andxor(a, b); if (result > bestResult) { bestResult = result; c = b; } } return c; } int main() { // First test case uvec a{ 3, 4, 5 }; uvec b{ 6, 7, 8 }; uvec c = solvePermute(a, b); uint bestResult = andxor(a, c); std::cout << "Maximum result is " << bestResult << " with c = "; for (uint ci : c) { std::cout << ci << " "; } return 0; } 。然后,不要在粒子上使用 Play on AwakePlay,而是使用粒子的 GameObject 并分别使用 Stop true 和 false。