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.