Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by kardamzaimov · May 10, 2018 at 06:03 PM · collisioncollidersoundcollision detectionaudiosource

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!

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Harinezumi · May 10, 2018 at 06:42 PM 0
Share

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.

avatar image kardamzaimov Harinezumi · May 10, 2018 at 09:25 PM 1
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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;
     }
 
 }
Comment
Add comment · Show 10 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image kardamzaimov · May 11, 2018 at 10:16 AM 0
Share

I get

 *Assets/volontrig.cs(6,14): error CS0101: The namespace `global::' already contains a definition for `SetVolumeOnCollision*


avatar image Harinezumi kardamzaimov · May 11, 2018 at 10:45 AM 0
Share

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).

avatar image kardamzaimov Harinezumi · May 11, 2018 at 04:00 PM 0
Share

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.

Show more comments
avatar image kardamzaimov · May 13, 2018 at 02:02 PM 0
Share

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?

avatar image Harinezumi kardamzaimov · May 13, 2018 at 10:36 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

142 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges