I am new to coding and I am still trying to learn and I got stuck with a couple problems I’m having. I’m making a game surround a spaceship that I made in blender. This ship has eight thrusters on it which is where I am having my problem. I want my particle system to activate when I press down a certain key and then to stop when the key is lifted. I have gotten the thrusters to activate when the key is pressed and deactivate when the key is pressed again, but OnKeyUp is not working for me. I would also like an audio clip to play only while the key is pressed down as well, again it activates, but does not shut off. Below is a copy of the code I’m working with:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Audio;
public class PLAYERMOVEMENT : MonoBehaviour {
public float PlayerUp = 5.0f;
public float PlayerRight = 5.0f;
public float PlayerForward = 5.0f;
public bool ThrustersActive = false;
public Rigidbody PlayerBody;
public ParticleSystem Left1;
public ParticleSystem Left2;
public ParticleSystem Right1;
public ParticleSystem Right2;
public ParticleSystem Forward1;
public ParticleSystem Forward2;
public ParticleSystem Backward1;
public ParticleSystem Backward2;
void FixedUpdate() {
//Player Horizontal Movement
if (Input.GetKeyDown( KeyCode.W )) //Forward Movement
{
ThrusterToggle();
PlayerBody.AddForce(0, 0, PlayerForward * Time.deltaTime);
ThrusterDeactivation();
}
if (Input.GetKeyDown( KeyCode.S )) //Backwards Movement
{
ThrusterToggle();
PlayerBody.AddForce(0, 0, -PlayerForward * Time.deltaTime);
}
if (Input.GetKeyDown( KeyCode.A )) //Left Movement
{
ThrusterToggle();
PlayerBody.AddForce(-PlayerRight * Time.deltaTime, 0, 0);
}
if (Input.GetKeyDown( KeyCode.D )) //Right Movement
{
ThrusterToggle();
PlayerBody.AddForce(PlayerRight * Time.deltaTime, 0, 0);
}
//Player Vertical Movement
if (Input.GetKeyDown( KeyCode.Q )) //Upward Movement
{
ThrusterToggle();
PlayerBody.AddForce(0, PlayerUp * Time.deltaTime, 0);
}
}
public void ThrusterDeactivation () {
//Horizontal Thruster Deactivation
if (Input.GetKeyUp( KeyCode.W ))
{
ThrusterToggle();
}
if (Input.GetKeyUp( KeyCode.S ))
{
ThrusterToggle();
}
if (Input.GetKeyUp( KeyCode.A ))
{
ThrusterToggle();
}
if (Input.GetKeyUp( KeyCode.D ))
{
ThrusterToggle();
}
//Vertical Thruster Deactivation
if (Input.GetKeyUp( KeyCode.Q ))
{
ThrusterToggle();
}
}
//Thruster Activation Toggle
public void ThrusterToggle() {
//Forward Thrusters Toggle
if (Input.GetKey( KeyCode.W ))
{
if (Forward1.isStopped)
{
Forward1.Play();
Forward2.Play();
ThrustersActive = true;
AudioCaller();
}
else
{
Forward1.Stop();
Forward2.Stop();
ThrustersActive = false;
AudioCaller();
}
}
//Backward Thrusters Toggle
if (Input.GetKey( KeyCode.S ))
{
if (Backward1.isStopped)
{
Backward1.Play();
Backward2.Play();
}
else
{
Backward1.Stop();
Backward2.Stop();
}
}
//Right Thrusters Toggle
if (Input.GetKey( KeyCode.A ))
{
if (Right1.isStopped)
{
Right1.Play();
Right2.Play();
}
else
{
Right1.Stop();
Right2.Stop();
}
}
//Left Thrusters Toggle
if (Input.GetKey( KeyCode.D ))
{
if (Left1.isStopped)
{
Left1.Play();
Left2.Play();
}
else
{
Left1.Stop();
Left2.Stop();
}
}
//Upward Thrusters Toggle
if (Input.GetKey( KeyCode.Q ))
{
if (Forward1.isStopped)
{
Forward1.Play();
Forward2.Play();
Backward1.Play();
Backward2.Play();
Left1.Play();
Left2.Play();
Right1.Play();
Right2.Play();
}
else
{
Forward1.Stop();
Forward2.Stop();
Backward1.Stop();
Backward2.Stop();
Left1.Stop();
Left2.Stop();
Right1.Stop();
Right2.Stop();
}
}
}
public void AudioCaller() {
if ( ThrustersActive == true )
{
FindObjectOfType<AudioManager>().PlayAudio("ThrustersActivate");
}
else if ( ThrustersActive == false )
{
FindObjectOfType<AudioManager>().StopAudio();
}
}
}