Search code examples
unity-game-engineparticle-system

Unity Particle System Issue


I'm making a racing game and want there to be rocks that fly off the tires when you go offroad. The problem I'm having is with the particle system not firing when it should. I use this for the ground to trigger a bool and the bool fires fine.

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

The weird thing is, the debug logs fire but the particle system doesn't. I've tried putting it all under the ground script, I've put it under the "isOnGround" under the "!isOnBoostStrip", and even though the acceleration swap works the particles don't I tried having no "p.Stop();" and just have it not loop and still no luck, although when I did that it did a weird thing where it would fire but only when I touched an obstacle.

Any help is most appreciated, I'm not sure where I'm going wrong.

Edit: In this video, I show the plane settings and the error with my current rocks system and with a default particle system. It also only checks if "IsOnGround" is true which seems to work just fine. Here is the video - https://www.youtube.com/watch?v=P6TfacNZZxE Here's the code I'm using in the video.

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

}

Solution

  • Unity's particle system can be a bit wonky sometimes as I'm sure you have now found out. A solution I have used in the past is setting the troublesome particle system to be Play on Awake. Then instead of using the Play and Stop on the particle, use the GameObject of the particles and use SetActive true and false respectively.