Can someone please help me out, I am going nuts!

This is my code, I am trying to shoot some particles out of the rocket (following rocket boost course) but no particles come out. I’ve assigned them in the inspector and everything. Just doesnt seem to play. I also checked with enginVFX.isPlaying and it returns false…


using System;

using System.Collections;

using UnityEngine;

public class Rocket : MonoBehaviour {

[SerializeField] float rcsThrust = 100f;

[SerializeField] float mainThrust = 100f;

[SerializeField] AudioClip mainEngine;

[SerializeField] AudioClip success;

[SerializeField] AudioClip death;

[SerializeField] ParticleSystem deathVFX;

[SerializeField] ParticleSystem engineVFX;

[SerializeField] ParticleSystem successVFX;

Rigidbody rigidBody;

AudioSource audioSource;

LevelLoader levelLoader;

enum State { Alive, Dying, Transcending }

State state = State.Alive;

bool collisionToggleEnabled = true;

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

    }

    if (Debug.isDebugBuild){

        RespondToDevInput();

    }

}

private void RespondToDevInput()

{

    if (Input.GetKeyDown(KeyCode.C)){

        collisionToggleEnabled = false;

        print("Collider disabled");

    }

}

void OnCollisionEnter(Collision collision)

{

    if (state != State.Alive || !collisionToggleEnabled) { return; }

    switch (collision.gameObject.tag)

    {

        case "Friendly":

            // do nothing

            break;

        case "Finish":

            StartSuccessSequence();

            break;

        default:

            StartDeathSequence();

            break;

    }

}

private void StartSuccessSequence()

{

    state = State.Transcending;

    audioSource.Stop();

    AudioSource.PlayClipAtPoint(success, transform.position);

    ParticleSystem successVFX_ = Instantiate(successVFX, transform.position, Quaternion.identity);

    successVFX_.Play();

    FindObjectOfType<LevelLoader>().LoadNextLevel();

    Destroy(gameObject);

}

private void StartDeathSequence()

{

    state = State.Dying;

    audioSource.Stop();

    AudioSource.PlayClipAtPoint(death, transform.position);

    ParticleSystem deathVFX_ = Instantiate(deathVFX, transform.position, Quaternion.identity);

    deathVFX_.Play();

    FindObjectOfType<LevelLoader>().ReloadLevel();

    Destroy(gameObject);

}

private void RespondToThrustInput()

{

    if (Input.GetKey(KeyCode.Space)) 

    {

        ApplyThrust();

    }

    else

    {

        audioSource.Stop();

        engineVFX.Stop();

    }

}

private void ApplyThrust()

{

    engineVFX.Play();

    rigidBody.AddRelativeForce(Vector3.up * mainThrust * Time.deltaTime);

    if (!audioSource.isPlaying) // so it doesn't layer

    {

        audioSource.PlayOneShot(mainEngine);

    }

}

private void RespondToRotateInput()

{

    rigidBody.freezeRotation = true; // take manual control of rotation

   

    float rotationThisFrame = rcsThrust * Time.deltaTime;

    if (Input.GetKey(KeyCode.A))

    {

        transform.Rotate(Vector3.forward * rotationThisFrame);

    }

    else if (Input.GetKey(KeyCode.D))

    {

        transform.Rotate(-Vector3.forward * rotationThisFrame);

    }

    rigidBody.freezeRotation = false; // resume physics control of rotation

}

}

For clarity sake, all the particle systems work fine, except for engine VFX…

Hi Marc,

Have you already compared your code to the Lecture Project Changes which can be found in the Resources of this lecture? Are there any error messages in your console?

I’ve literally copied the lecture code now. No error messages. Still not working. So strange…

Try to wrap the Play method inside an if-block like you did with the PlayOneShot method. The particle system has got an isPlaying property, too.

Also add Debug logs to your code to see whether your methods/ code blocks get called.

Thanks. I tried calling isPlaying() right after I called the .Play() function. But it logs ‘false’. So it seems that the .Play() doesn’t actually play the deathVFX. No clue how to debug this further…

Could you share the relevant code snippet as formatted text so I know what exactly you did? Where did you add Debug.Logs?

What happens when you select the particle system in your Hierarchy and click on the Play button in the scene view? Does the particle system emit particles? If not, the issue is probably not in your code but in the settings of the particle system. In that case, please share screenshots of the particle system settings.

Last but not least check if you assigned the particle system from the Hierarchy to your Rocket component. Maybe a prefab is assigned.

Hi Nina,

I finally found it. It turned out I messed up during refactoring.

Thanks!

Marc

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

Privacy & Terms