How to play multiple sounds for different thrusters, at the same time?

I am trying to add a different sound for the RCS thrusters (Rotation)
I have made a [SerializeField] value for the “SideThruster” and added the soundclip.

But I cannot get it to work the same as the main thruster sound.

If I use the !audioSource.isPlaying in the “Rotation” function the mainthruster sound stops working and if I hold thrust and rotate I can hear the different sounds sometimes, but not consistent.

What I want is that if you thrust up, and rotate, you hear the Main Thruster Sound, and the side thruster sound at the same time / layered.

I just cannot figure it out and its driving me nuts !

    private void RespondtoThrustInput()
{
    if (Input.GetKey(KeyCode.Space))
    {
        ApplyThrust();
    }
    else
    {
        audioSource.Stop();                                         // so audio only plays when you press space, stopping the audio on button release
    }
}

private void ApplyThrust()
{
    float thrustThisFrame = mainThrust * Time.deltaTime;            // Time.deltaTime makes a calculation based off the last frame time which is a good indicater for the next frametime.
    rigidBody.AddRelativeForce(Vector3.up * thrustThisFrame);
    if (!audioSource.isPlaying)                                 // ! means is not playing
    {
        audioSource.PlayOneShot(MainThruster);
    }
}

private void RespondtoRotationInput()
{
    float rotationThisFrame = rcsRotate * Time.deltaTime;
    rigidBody.freezeRotation = true;                                // Take manual control of rotation -> prevent physics from spinning your rocket
    if (Input.GetKey(KeyCode.A))
    {
        transform.Rotate(Vector3.forward * rotationThisFrame);          // forward because unity uses left-handed coordination system
    }
    else if (Input.GetKey(KeyCode.D))
    {
        transform.Rotate(Vector3.back * rotationThisFrame);
    }
    rigidBody.freezeRotation = false;                           // Resume physics control.
}

You’ll need a second AudioSource component on your rocket, since each AudioSource can only play a single clip at a time.

1 Like

Thanks !

That was indeed the way to fix it.
Add 2 sound components to the rocket.

AudioSource mainThruster;
AudioSource sideThruster;

AudioSource[] ThrusterSounds = GetComponents<AudioSource>();
mainThruster = ThrusterSounds[0];
sideThruster = ThrusterSounds[1];

This is needed if you want to play 2 sounds - at the same time - without stopping the other sound.

1 Like

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

Privacy & Terms