Particle effects

using UnityEngine.SceneManagement;
using UnityEngine;
using System;

public class Rocket : MonoBehaviour
{
enum State {Alive, Dying, Transcending};
State state = State.Alive;
[SerializeField]
float rcsThrust = 100f;
[SerializeField]
float mainThrust = 100f;
[SerializeField]
AudioClip mainEngine;
[SerializeField]
AudioClip death;
[SerializeField]
AudioClip nextLevel;

[SerializeField]
ParticleSystem mainEngineParticles;
[SerializeField]
ParticleSystem deathParticles;
[SerializeField]
ParticleSystem nextLevelParticles;

AudioSource audioSource;
Rigidbody rigidBody;
// Start is called before the first frame update
void Start()
{
    audioSource = GetComponent<AudioSource>();
    rigidBody = GetComponent<Rigidbody>();
}

// Update is called once per frame
void Update()
//switch off audio after dying
{
    if(state == State.Alive){
        Thrust();
        Rotate();
    }
    }

void OnCollisionEnter(Collision collision)
{
    if (state != State.Alive) { return; }
    switch (collision.gameObject.tag)
    {
        
        case "Friendly":
            //do nothing
            break;
        case "Finish":
            StartSuccesSequence();
            break;
        default:
            StartDeathSequence();
            break;
    }
}

private void StartDeathSequence()
{
    state = State.Dying;
    audioSource.Stop();
    DyingSound();
    deathParticles.Play();
    Invoke("LoadFirstScene", 3f);
}

private void StartSuccesSequence()
{
    state = State.Transcending;
    audioSource.Stop();
    NextLevelSound();
    nextLevelParticles.Play();
    Invoke("LoadNextScene", 3f);
}

private void DyingSound()
{
    audioSource.PlayOneShot(death);
}

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

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

private void NextLevelSound()
{
    audioSource.PlayOneShot(nextLevel);
}

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

    }
}

private void ApplyThrust()
{
    rigidBody.AddRelativeForce(Vector3.up * mainThrust);
    if (!audioSource.isPlaying)
    {
        audioSource.PlayOneShot(mainEngine);
    }
    mainEngineParticles.Play();
}

private void Rotate()
{

    float rotationThisFrame = rcsThrust * Time.deltaTime;
    rigidBody.freezeRotation = true; // take manual control of rotation
    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

}

}

Privacy & Terms