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