0
votes

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

}
1
Are your boost strip particles playing when they go over a boost? Or are no particle systems working? - TEEBQNE
That's the strange part, my other particle systems all work great, I have them on the wheels and the exhaust and they work fine using the same "foreach". So I tried to play the exhaust particles instead of the ground ones in this instance, and they operate with the same weird behavior. Below someone suggested that I see if unity thinks the particles system is playing with a debug which I did. It shows that it's playing but it isn't. I made a vid of it so you can see the weird behavior. It thinks it's playing but isn't then will start when I bump an object. youtube.com/watch?v=8M7eHxW006A - poprocklex
At the beginning of the vid, you can see my drift particle system fire just fine. If you don't want to watch the whole thing, the rocks start working at the 20-sec mark. - poprocklex
Gotcha. Something I noticed in the video is it's not just when you hit an obstacle, but you appeared to get some air when you did hit it. And when you are driving on the ground, is the isWorking debug printing every frame? Is your ground tagged and/or layered properly to how you are doing your ground check? Your particle systems seem fine as they work, it is some logic leading up to and looping through on the Start of the particles. - TEEBQNE
First off, thanks for helping me out! That's true with the air thing, so I switched my code to just check for "if (isOnGround)", now if I remove this the particle system will play non-stop but error-free. I'm going to edit above to show my current code and a new video that will show what's happening. I also made a default part sys and it still has the same bugs. - poprocklex

1 Answers

0
votes

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.