Rocket Particles won't work correctly

Everyone, I’ve struggled for days to get my rocket particles to work to no avail. I’ve dragged the particles from the rocket child to the selection spot in the inspector and that didn’t work. I’ve messed around the prefab, and even tried it without any prefabs existing and still can’t get it to work. When I’m firing my rocket, I will occasionally (about once every 3 or 4 seconds) see a single particle from the effect appear. Please does anyone have any idea what could be causing this? My code below:

using UnityEngine;
using UnityEngine.SceneManagement;

public class Rocket : MonoBehaviour
{
    //todo fix lighting bug
    [SerializeField] float rcsThrust = 200f;
    [SerializeField] float mainThrust = 900f;
    [SerializeField] float levelLoadDelay = 2f;

    [SerializeField] AudioClip mainEngine; //must drag and drop in unity inspector
    [SerializeField] AudioClip deathSound;
    [SerializeField] AudioClip levelChangeSound;

    [SerializeField] ParticleSystem mainEngineParticles; // must drag and drop in unity inspector
    [SerializeField] ParticleSystem deathSoundParticles;
    [SerializeField] ParticleSystem levelChangeSoundParticles;

    Rigidbody rigidBody; //member variable set to access Rigidbody to move ship
    AudioSource audioSource;

    enum State { Alive, Dying, Transcending } //making a variable of the 3 types
    State state = State.Alive;

	// Use this for initialization
	void Start ()
    {
        rigidBody = GetComponent<Rigidbody>();
        audioSource = GetComponent<AudioSource>();
	}
	
	// Update is called once per frame
	void Update ()
    {
        if (state == State.Alive)
            {
            RespondToThrustInput();
            RespondToRotateInput();
            }
        
    }

    void OnCollisionEnter(Collision collision)
    {
        if (state != State.Alive) { return; } // guard condition (only run if alive)

        switch (collision.gameObject.tag) //when colliding with a gameobject, do something based on tag
        {
            case "Friendly":
                audioSource.Stop();
                state = State.Alive;
                break;
            case "Finish":
                StartSuccessSequence();
                break;
            default:
                StartDeathSequence();
                break;
        }
    }

    private void StartSuccessSequence()
    {
        state = State.Transcending;
        audioSource.Stop();
        audioSource.PlayOneShot(levelChangeSound);
        levelChangeSoundParticles.Play();
        Invoke("LoadNextScene", levelLoadDelay); //parameterise this time; delays 1 second
    }
    private void StartDeathSequence()
    {
        state = State.Dying;
        audioSource.Stop();
        audioSource.PlayOneShot(deathSound);
        deathSoundParticles.Play();
        Invoke("LoadFirstScene", levelLoadDelay);
    }

    private void LoadNextScene()
    {
        SceneManager.LoadScene(1);        
    }

    private void LoadFirstScene()
    {
        SceneManager.LoadScene(0);
    }

    private void RespondToThrustInput()
    {
      //  float thrustThisFrame = mainThrust * Time.deltaTime; //increase or decrease thrust

        if (Input.GetKey(KeyCode.Space)) //can thrust while turning
        {
            ApplyThrust();
        }
        else
        {
            audioSource.Stop();
            mainEngineParticles.Stop();
        }
    }

    private void ApplyThrust()
    {
        rigidBody.AddRelativeForce(Vector3.up * mainThrust * Time.deltaTime);
        if (!audioSource.isPlaying) // so it doesn't layer (keep starting over)
        {
            audioSource.PlayOneShot(mainEngine);
        }
        mainEngineParticles.Play();
    }

    private void RespondToRotateInput()
    {
        rigidBody.freezeRotation = true; // take control of the rotation
        float rotationThisFrame = rcsThrust * Time.deltaTime; // increase or decrease rorate speed

        if (Input.GetKey(KeyCode.A))
        {
            transform.Rotate(Vector3.forward * rotationThisFrame); // rotates ship
        }
        else if (Input.GetKey(KeyCode.D))
        {
            transform.Rotate(-Vector3.forward * rotationThisFrame);
        }

        rigidBody.freezeRotation = false; //resume physics control
    }

}

Also, If I’m pressing thrust when I die, then I see the particles.

edit: If I comment out the code that stops the particles, then they play whenever I’m no longer trusting… ugh, there is a minor error somewhere and I can’t figure it out.

Hmmm a quick glance at your code, and I’m not seeing a problem… Is there any output in the console when you run it? If you’re not getting anywhere, create a unitypackage and find a place to host it and I can take a look.

Thanks for taking a look. Nothing prints to console when I run it, and the problem is the same before I wrote the oscillator code - so I think its either the rocket code or the rocket itself. I’m out of town visiting family, but will check to see if I can host a unitypackage when I get back. I don’t know what that means yet lol, but I’ll google it. Until then I’ll keep seeing if I can figure it out.

Thanks again.

Can you put in print functions in following ->

    if (Input.GetKey(KeyCode.Space)) //can thrust while turning
    {
        ApplyThrust();
        print("Thrusting");
    }
    else
    {
        audioSource.Stop();
        mainEngineParticles.Stop();
        print("STOP thrusting");
        print("Thrusting");
    }

Look if they are booth being called at the same time? or just after each other for some weird reason

Hi,

Couple of thoughts;

  • I think it was the Block Breaker game where a lot of students had problems with the Smoke Puff particles not being displayed, this was typically because the Particles GameObject was rotated in such a way that they were going into the screen, this was obviously a 2D game not a 3D game but may be worth checking.

  • Another common issue is that often the transform hasn’t been reset when added to an existing GameObject, this, for example, could mean that whilst the structure of the space ship look ok in the Hierarchy, your particles could be way off to the left or right for example, thus, appearing but outside of the viewport of the camera.

  • Another considering is the Emissions options within the Particle System, what are these currently set to? I think 100 was the example given, but I can create the same issue as you have described if I change this to 1. Then, when thrust is applied, and stop, and re-applied, particles typically are not generated, but if I apply thrust and send the rocket into the terrain, after a second a single particle appears.

If none of the above issues apply, and you have checked to see that the Play method is definitely being called then I would suggest zipping up the project files, or creating the Unity Package as suggested above and letting someone else take a quick look at the project, often a second pairs of eyes on something you’ve been looking at for too long can be very helpful, but it can be quite difficult sometimes to diagnose without having access to the whole project.

3 Likes

It was option 3 from above, kind of. The emissions was turned on. I went in and checked the setting and it was set to 100. However, the rate over distance was set to 0. I’m not sure what it is supposed to be, but cranking it up to 50 fixed the problem completely.

Thank you everyone for your help and special thanks to Rob for figuring it out.

Strangely, I was careful not to change any settings. I must have fat-fingered it at some point.

3 Likes

Glad you have it resolved and can move forward again John :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms