UnityGameProjectsCode / RelianceGame / Helper Scripts / PlaySoundOnCollision.cs
PlaySoundOnCollision.cs
Raw
using UnityEngine;

public class PlaySoundOnCollision : MonoBehaviour //placed on objects that have rigid bodies. Plays a sound when the object hits into things or is hit into
{
    AudioSource audioS;
    public AudioSource audioSPub;

    public bool multipleSources = false;

    private void Awake()
    {
        audioS = GetComponent<AudioSource>();
    }

    private void OnCollisionEnter(Collision collision)
    {
        if (!multipleSources)
            audioS.Play();
        else
            audioSPub.Play();
    }
}