Problem with play/stop feature

so im having this same issue with the booster audio and the particle system for the main booster. i have the code written the same way with the if statement that states if its not playing do this function but when i go into unity the audio and particle system play when i press the spacebar and continues playing until i press the spacebar again why is my code doing this when it works right for rick?
for simpler terms in case you dont understand:
press space (audio booster sound and main particles play and dont stop)
press space again (both turn off and dont start)
press space again (audio booster sound and main particles play and dont stop)
press space again (both turn off and dont start)
press space again (audio booster sound and main particles play and dont stop)
press space again (both turn off and dont start)
and this is how it works for me in unity but it seems with rick it starts when he presses space and turns off when he stops pressing it it doesnt loop continuously until he presses again

One thing to check is that you’re getting the input using Input.GetKey(KeyCode.Space) and not Input.GetKeyDown(KeyCode.Space)

GetKey will be in effect while the key is being held down, while GetKeyDown will trigger once, every time the key is pressed, ignoring how long the key is held.

Another possible issue might be having the else contained within the first if statement rather than after it. The code should look like this:

if(Input.GetKey(KeyCode.Space))
{
    rb.AddRelativeForce(Vector3.up * mainThrust * Time.deltaTime);
    if(!audioSource.isPlaying)
    {
        audioSource.PlayOneShot(mainEngine);
    }
    if(!mainEngineParticles.isPlaying)
    {
        mainEngineParticles.Play();
    }
}
else // Else is here, not contained in the If check
{
    audioSource.Stop();
    mainEngineParticles.Stop();
}

heres my code as it is now i used input getkeydown because based off what vs said getkey was for if you hold the button down while getkeydown returns an action after it is pressed and i thought for the booster we wanted to keep pressing it not holding it down. also now after i finished extracting my method for all the inputs the game doesnt even wanna make a sound or do the particle effects when i do press space

using System.Collections;
using System.Collections.Generic;
using System.Threading;
using UnityEngine;
using UnityEngine.UIElements;

public class Movement : MonoBehaviour
{
Rigidbody playerRb;
[SerializeField] int thrustForce = 20000;
[SerializeField] float rotationSpeed = 50f;
AudioSource rocketAudio;
[SerializeField] ParticleSystem leftBooster;
[SerializeField] ParticleSystem rightBooster;
[SerializeField] ParticleSystem mainBooster;

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

// Update is called once per frame
void Update()
{
    ProcessThrust();
    ProcessRotation();
}

void ProcessThrust()
{
    if (Input.GetKeyDown(KeyCode.Space))
    {
        StartThrusting();
    }
        else
    {
        StopThrusting();
    }
}

private void StopThrusting()
{
    rocketAudio.Stop();
    mainBooster.Stop();
}

private void StartThrusting()
{
    playerRb.AddRelativeForce(Vector3.up * thrustForce * Time.deltaTime);
    if (!rocketAudio.isPlaying)
    {
        rocketAudio.Play();
    }
    if (!mainBooster.isPlaying)
    {
        mainBooster.Play();
    }
}

void ProcessRotation()
    {
    if (Input.GetKey(KeyCode.A))
        RightRotation();
    else if (Input.GetKey(KeyCode.D))
        LeftRotatation();
    else
    {
        StopParticles();
    }
}

private void StopParticles()
{
    leftBooster.Stop();
    rightBooster.Stop();
}

private void LeftRotatation()
{
    ApplyRotation(-rotationSpeed);
    if (!rightBooster.isPlaying)
    {
        rightBooster.Play();
    }
}

private void RightRotation()
{
    ApplyRotation(rotationSpeed);
    if (!leftBooster.isPlaying)
    {
        leftBooster.Play();
    }
}

void ApplyRotation(float rotationThisFrame)
    {
        playerRb.freezeRotation = true;
        transform.Rotate(Vector3.forward * rotationThisFrame * Time.deltaTime);
        playerRb.freezeRotation = false;
    }

}

i just updated my code to getkey and it works just fine now too thank you!

1 Like

I’m glad it helped! :smiley:

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

Privacy & Terms