If / If-not statement on particles

Hello there, i got a quick question.

I did not add the if-statement so the particles would turn off, because they already behave like they should (only thrusting when the button is pressed). But why is that?
Not that its not nice that they do, but its kind of a mystery.

So here’s the code:

using UnityEngine;

public class Movement : MonoBehaviour
{
[SerializeField] float mainThrust = 500.0f;
[SerializeField] float sideThrust = 100.0f;
[SerializeField] ParticleSystem leftThruster;
[SerializeField] ParticleSystem rightThruster;
[SerializeField] ParticleSystem mainThruster;

[SerializeField] AudioClip mainEngine;


AudioSource audioSource;
Rigidbody rb;

void Start()
{
    rb = GetComponent<Rigidbody>();
    audioSource = GetComponent<AudioSource>();
}

void Update()
{
    ProcessThrust();
    ProcessRotation();
}
void ProcessRotation()
{
    if (Input.GetKey(KeyCode.A))
    {
        ApplyRotation(sideThrust);
        leftThruster.Play();
    }
    else if (Input.GetKey(KeyCode.D))
    {
        ApplyRotation(-sideThrust);
        rightThruster.Play();
    }
}
void ApplyRotation(float rotationThisFrame)
{
    rb.freezeRotation = true;
    transform.Rotate(Vector2.left * rotationThisFrame * Time.deltaTime);
    rb.freezeRotation = false;
}

void ProcessThrust()
{
    if (Input.GetKey(KeyCode.Space))
    {
        mainThruster.Play();
        rb.AddRelativeForce(Vector3.up * mainThrust * Time.deltaTime);
        if (!audioSource.isPlaying)
        {
            audioSource.PlayOneShot(mainEngine);
            
        }
    }
    else
    {
        audioSource.Stop();
    }
}

}

1 Like

Hi! Welcome to the community!

If the code is exactly the same and it’s working, then this probably has something to do with your particle settings more than the code.Try comparing yours with Rick’s and see what’s different.

1 Like

Hi Mishu,

Welcome to our community! :slight_smile:

In addition to what Yee wrote, I’d like to mention that Unity occasionally makes significant changes in their game engine. Depending on your version of Unity, it might be that components behave in a slightly different way than Gary’s or Rick’s.

Furthermore, depending on the hardware and also on “luck”, it might be that you do not experience certain problems Gary and Rick encounter. Unfortunately, not noticing issues other people get is a common problem in programming. If you ever played a buggy game, you know what I mean.

For this reason, I would recommend to understand the problems Gary and Rick are trying to solve in the videos, and to implement their fix if you feel that your future players might encounter the problem, too.

I hope this helped. :slight_smile:


See also:

1 Like

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

Privacy & Terms