- Home /
Collider + multiple sounds script
Hi!
I am working on a project that mainly needs to have 4-5 objects, each with a different sound track on them, that will play upon touch (collision) with a first person character walking around.
Since all 5 sounds will be connected to each other (one will be bass, one vocals, one guitar etc.) and they actually need to play synchronized , I need them all to play on awake, but muted. When the character collides with an objects, the sound that is attached to that object can just un-mute.
I am an architect, I designed an amazing geometry and need an interactive way to display it. I have very very limited coding skills and even after looking at unity documentation, still don't have an exact idea how the script would work!
Any help would be enormously appreciated!
What would you like to do, continuously play the sound until the player touches them? Or play the sound only when the player touches them? Your description is conflicting.
I can help with the script, but I need to understand what you want to do.
At the beginning there is no sound. You have 5 objects. If you touch one of the objects, a specific sound plays continuously even if you go away from it. Every object plays a different sound.
Lets say you already collided with two objects and you can hear two sounds. One of them is drums sound , one of them is guitar sound. If the audio starts playing upon collision, the second sound will most probably not match the first one (because it was triggered to start at a random time). That is why I want all sounds to start at the beginning, muted and collision to un-mute them.
Answer by Harinezumi · May 11, 2018 at 07:30 AM
Thanks for the explanation, now I see what you want.
It is actually pretty easy to do this: you just need a script that sets volume to 0 in the beginning, and to 1 (or whatever you would like) when there is a collision with a specific object. For the collision to work, you also need to set up the objects correctly (at least one has to have a rigidbody). For example, your player would have a collider (CapsuleCollider or CharacterController) and a rigidbody (probably set to kinematic), while the sound objects just a collider, like a BoxColldier or SphereCollider.
Here is the script. I will also try to explain what each line does so you can learn from it and become better at programming ;)
using UnityEngine;
// make sure the game object you attach it to has an AudioSource on it
[RequireComponent(typeof(AudioSource))]
// create a script that you can attach to a game object
public class SetVolumeOnCollision : MonoBehaviour {
// the volume to set when collided; can be set in the Editor;
// not necessarily needed, but it will make the component more versatile
[SerializeField] private float volumeOnCollision = 1;
// the tag to which it should react; also not essential, but nice to have
// make sure you set the tag of your colliding object if you do use it!
[SerializeField] private string tagToReactTo = "Player";
// the audio source you want to control
private AudioSource audioSource;
// this is called in the first moment, even before sounds start to play
private void Awake () {
// get the audio source to control that is on this game object
audioSource = GetComponent<AudioSource>();
// set volume to 0, muting it; you can also do this in the Editor
audioSource.volume = 0;
// make sure it wil start to play; this as well can be set in the Editor
audioSource.playOnAwake = true;
}
// this function is called on the frame when something collides with this game object
private void OnCollisionEnter (Collision collision) {
// check the tag; remove this if you don't want it
if (collision.collider.CompareTag(tagToReactTo))
// set the desired voluem; just replace it with 1 if you don't want to use desired volume
audioSource.volume = volumeOnCollision;
}
}
I get
*Assets/volontrig.cs(6,14): error CS0101: The namespace `global::' already contains a definition for `SetVolumeOnCollision*
Remove the copy of the code from volontrig.cs
. This script should go into its separate file, and the file must be called SetVolumeOnCollision.cs
(don't add the extension if you create it from Unity).
Thank you so much for you help! I saved the script as SetVolumeOnCollision, it plays without any errors, but there is NO sound upon collision. I made a simple test with a Cube with an audio source and the script attached to it. For the player I use the FPSController prefab from Unity standard assets - it has a Rigidbody. If I remove the script, the audio is just playing from the start as it should, so I'm sure the audiosource is fine.
I just managed to make it run, used some strange FPS player and it works now!!! This is amazing, thank you so much for your help! BTW is there a simple way to mute it again upon second collision?
I'm glad it works!
Yes, there is a simple way: change the line audioSource.volume = volumeOnCollision;
to audioSource.volume = (audioSource.volume > 0) ? 0 : volumeOnCollision;
. This will always change the volume to 0 when it was greater than 0, and to the set volume when it is 0.
Your answer
Follow this Question
Related Questions
Avoid Player bouncing when colliding with objects 3D 1 Answer
How to set collision for an object with specific collider size? 3 Answers
Cubes collide with terrain 1 Answer
Why the npc character walking strange when using a Rigidbody and Is Kinematic on enabled ? 1 Answer
2D Platformer - Action When Key Is Pressed During Collision C# 0 Answers