Multiple sound problem please solve!

First of all i have read all the questions like this but I was not able to understand and i am really confused ! so I thought i should ask myself.

This is my code and when is stopped the audioSource so all the clips worked fine but the thrust never stopped-

but When made that the statement .Other sound’s stopped.

And This is The code-

`using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Rocket : MonoBehaviour
{
    AudioSource audioSource;
    Rigidbody rigidBody;
    public float Thrust_Speed = 1000;
    public float RotateValue = 200 ;
    public AudioClip Jingle;
    public AudioClip Death_Explosion;
    public AudioClip Thrust_Sfx;

    void Start()
    {
        audioSource = GetComponent<AudioSource>();
        rigidBody = GetComponent<Rigidbody>();
    }
    void Update()
    {
        audioSource.Play();
        Thrust();
        Rotate();
    }
    private void OnCollisionEnter(Collision collision)
    {
        
        if (collision.gameObject.tag == "Friendly")
        {
            print("Ok");
        }
        else if (collision.gameObject.tag == "Finish")
        {
            Invoke("Go_To_Next_Level", 1f);
            audioSource.PlayOneShot(Jingle);
        }
        else
        {
            Invoke("Died", 1f);
            audioSource.PlayOneShot(Death_Explosion);
        }
    }

    void Thrust()
    {
        float Thrust_Value = Thrust_Speed * Time.deltaTime;
        if (Input.GetKey(KeyCode.Space))
        {
            rigidBody.AddRelativeForce(Vector3.up * Thrust_Value);
            if(!audioSource.isPlaying)
            {
                audioSource.PlayOneShot(Thrust_Sfx);
            }
        }
        else
        {
            audioSource.Stop();
        }
        
    }
    void Rotate()
    {
        float RotateSpeed = RotateValue * Time.deltaTime;
        if (Input.GetKey(KeyCode.A))
        {
            transform.Rotate(Vector3.forward * RotateSpeed);
        }
        else if (Input.GetKey(KeyCode.D))
        {
            transform.Rotate(- Vector3.forward * RotateSpeed);
        }
    }
    
    void Died()
    {
        print("you dead");
    }
    void Go_To_Next_Level()
    {
        print("This is next level");
    }
}
`

Hi Shuklahari,

Do you mean the thrust or the thrust sound?

What other sounds do you mean?

I meant the thrust sound and the other sound’s are jingle chime,Death explosion .

PlayOneShot cannot be stopped. See here. Try to replace the method with Play.

1 Like

Thanks! the problem is solved. :slight_smile:

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

Privacy & Terms