Multiple audio

How would I add a sound to my thrusters on the rocket. I tried this: and it doesn’t work
using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class Movement : MonoBehaviour

{

[SerializeField] float mainThrust = 100f; 

[SerializeField] float rotationThrust = 1f;

[SerializeField] AudioClip mainEngine;

[SerializeField] AudioClip LeftThrusterAudio;

[SerializeField] AudioClip RightThrusterAudio;

[SerializeField] ParticleSystem mainEngineParticles;

[SerializeField] ParticleSystem leftThrusterParticles;

[SerializeField] ParticleSystem rightThrusterParticles;



Rigidbody rb;

AudioSource audioSource;

// Start is called before the first frame update

void Start()

{

    rb = GetComponent<Rigidbody>();

    audioSource = GetComponent<AudioSource>();

}

    

// Update is called once per frame

void Update()

{

    ProcessThrust();

    ProcessRotation();

}

void ProcessThrust()

{

    if (Input.GetKey(KeyCode.Space))

    {

        StartThrusting();

    }

    else

    {

        StopThrusting();

    }

}

void ProcessRotation()

{

    if (Input.GetKey(KeyCode.A))

    {

         audioSource.PlayOneShot(LeftThrusterAudio);

        RotateLeft();

    }

    else if ( Input.GetKey(KeyCode.D))

    {

         

        audioSource.PlayOneShot(RightThrusterAudio);

        RotateRight();

    }

    else

    {

        StopRotating();

    }

} 

void StartThrusting()

{

    rb.AddRelativeForce(Vector3.up * mainThrust * Time.deltaTime);

    if (!audioSource.isPlaying)

    {

        audioSource.PlayOneShot(mainEngine);

    }

    if (!mainEngineParticles.isPlaying)

    {

        mainEngineParticles.Play();

    }

}

void StopThrusting()

{

    audioSource.Stop();

    mainEngineParticles.Stop();

}

private void StopRotating()

{

    rightThrusterParticles.Stop();

    leftThrusterParticles.Stop();

}

private void RotateRight()

{

    ApplyRotation(-rotationThrust);

    if (!leftThrusterParticles.isPlaying)

    {

        

       leftThrusterParticles.Play();

    }

}

void RotateLeft()

{

    ApplyRotation(rotationThrust);

    if (!rightThrusterParticles.isPlaying)

    {

   

     rightThrusterParticles.Play();

    }

}

void ApplyRotation(float rotationThisFrame)

{

    rb.freezeRotation = true; //freezing rotation so that we can manually rotate

    transform.Rotate(Vector3.forward * rotationThisFrame * Time.deltaTime);

    rb.freezeRotation = false; // unfreezing rotation so the physics system can take over

}

}

Hi,

Your solution looks fine so far. Have you already tried to add Debug.Logs to your code to see what is going on during runtime? Maybe you’ll have to check if the audio source is already playing before calling the PlayOneShot method. Take a look at the AudioSource class in the API. There you’ll find what I mean.

Not sure why it only works when I’m boosting and it’s obviously really glitched lol

Good grief, my poor ears! :dizzy_face:

Did you try what I suggested? The if-conditions return true only while specific keys are being pressed down.

But the audio is supposed to play when you press specific keys. i thought playOneShot meant that I could add multiple audio sources. I’m confused. Also don’t know what API is

I think it’s because I’m missing !audio.isPlaying. But i don’t know where to put it

This is what I changed it to but it still doesn’t work

public class Movement : MonoBehaviour

{

[SerializeField] float mainThrust = 100f; 

[SerializeField] float rotationThrust = 1f;

[SerializeField] AudioClip mainEngine;

[SerializeField] AudioClip LeftThrusterAudio;

[SerializeField] AudioClip RightThrusterAudio;

[SerializeField] ParticleSystem mainEngineParticles;

[SerializeField] ParticleSystem leftThrusterParticles;

[SerializeField] ParticleSystem rightThrusterParticles;



Rigidbody rb;

AudioSource audioSource;

// Start is called before the first frame update

void Start()

{

    rb = GetComponent<Rigidbody>();

    audioSource = GetComponent<AudioSource>();

}

    

// Update is called once per frame

void Update()

{

    ProcessThrust();

    ProcessRotation();

}

void ProcessThrust()

{

    if (Input.GetKey(KeyCode.Space))

    {

        StartThrusting();

    }

    else

    {

        StopThrusting();

    }

}

void ProcessRotation()

{

    if (Input.GetKey(KeyCode.A))

    {

         RotateLeft();

    }

    else if ( Input.GetKey(KeyCode.D))

    {

         

        RotateRight();

    }

    else

    {

        StopRotating();

    }

} 

void StartThrusting()

{

    rb.AddRelativeForce(Vector3.up * mainThrust * Time.deltaTime);

    if (!audioSource.isPlaying)

    {

        audioSource.PlayOneShot(mainEngine);

    }

    if (!mainEngineParticles.isPlaying)

    {

        mainEngineParticles.Play();

    }

}

void StopThrusting()

{

    audioSource.Stop();

    mainEngineParticles.Stop();

}

private void StopRotating()

{

    rightThrusterParticles.Stop();

    leftThrusterParticles.Stop();

}

private void RotateRight()

{

    ApplyRotation(-rotationThrust);

    if (!leftThrusterParticles.isPlaying)

    {

        

       leftThrusterParticles.Play();

    }

     if (!audioSource.isPlaying)

    {

        audioSource.PlayOneShot(RightThrusterAudio);

    }

}

void RotateLeft()

{

    ApplyRotation(rotationThrust);

    if (!rightThrusterParticles.isPlaying)

    {

   

     rightThrusterParticles.Play();

    }

     if (!audioSource.isPlaying)

    {

        audioSource.PlayOneShot(LeftThrusterAudio);

    }

}

void ApplyRotation(float rotationThisFrame)

{

    rb.freezeRotation = true; //freezing rotation so that we can manually rotate

    transform.Rotate(Vector3.forward * rotationThisFrame * Time.deltaTime);

    rb.freezeRotation = false; // unfreezing rotation so the physics system can take over

}

}

The API is the place where you can look up what a language, library or framework offers. Here is the Unity API:

And here is the AudioSource class:

There is a list with things you can use and access. One of them is PlayOneShot.


I think it’s because I’m missing !audio.isPlaying. But i don’t know where to put it

I think you might be right. At least, you should test your idea. From what I see, you already implemented the relevant if-conditions. At least, your code looks correct.

Please check how many audio sources there are in your Hierarchy. Also check if there are multiple Movement scripts in your Hierarchy. Check all game objects.

If you cannot find any duplicated Movement scripts, add a few Debug.Logs to your code to figure out if your if-conditions are working. They look correct but we cannot see if they work as expected. A lot is going on during runtime. Don’t forget to add a meaningful message to the Debug.Log. Otherwise, it’ll be difficult to interpret the output in the console.

In the Start method, add another Debug.Log to figure out how many Movement scripts there are in your scene during runtime.

And if nothing helps, comment out all lines of code that are supposed to play an audio clip. Test your game to hear if the problem persists. Before wasting too much time in the wrong place, always check if you are in the right place. If the problem is gone after you commented out the lines, you can be sure that the problem is very likely caused by the lines you commented out. In that case, re-add one of the lines and test your game again. If the problem does not appear, re-add another line and test the game again until the problem appeared again.

Yes, I know that debugging can be fairly repetitive and boring but that’s part of the job, so don’t give up. :slight_smile: