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 12:12 PM · collisionsoundscolidermute

Sound volume on different colisions

I have a sound and an object to collide with. I want the sound to play (but muted) on awake, but to un-mute on collision.

Tbh, I have no idea how to script it. Can someone help, I tried using unity library and coding it myself, but I get serious compiler errors all of the time.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Ricardo100 · May 10, 2018 at 12:33 PM

You can create an audio manager and create a reference of the audiosource that changes the volume according to the collision. Or you can just use the functions Stop() and Play()

 public AudioSource asource;
 
 
 void Awake()
 {
 asource.volume = 0;
 }
 
 void OnCollisionEnter(Collision collision)
 {
         if (collision.tag == "Object") // Tag of the gameobject that contains this collider 
             audioSource.volume = 1;
     }

I didn't tested the script but I guess you can have an idea of how it works.... Some Unity documentation references: AudioSource: https://docs.unity3d.com/ScriptReference/AudioSource.html

Collision: https://docs.unity3d.com/ScriptReference/Collider.OnCollisionEnter.html

Be aware of the collision types, if you are using a trigger the callback name changes to OnTriggerEnter instead of OnCollisionEnter.

Comment
Add comment · Show 4 · 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 10, 2018 at 12:47 PM 0
Share

Thanks! I was already looking at the documentation, for both audio source and collides, but I can't seem to write the hole script on my own. I am really a beginner at this.

avatar image kardamzaimov · May 10, 2018 at 12:50 PM 0
Share

I use mute, ins$$anonymous$$d of start/stop, because I have different audio sources (from different collisions) that should play together in harmony. Start/stop will just play them randomly and the mix of them will be chaotic.

avatar image Ricardo100 kardamzaimov · May 10, 2018 at 12:55 PM 0
Share

There is a more complex way but it will save time on the future, study about Resources.Load() so you can use a single audiosource but load a new audioclip when you need a new clip, or just create prefabs of these sounds and instantiate them and destroy after a few seconds as you need. Good luck with your project ;)

avatar image kardamzaimov Ricardo100 · May 10, 2018 at 01:08 PM 0
Share

can you help me with the basic script you have just begun on your answer?

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

128 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

Related Questions

Box Colider not working properly 1 Answer

Playing a sound proportionally to the force of an impact, via C# script 1 Answer

My Position suddenly changes 0 Answers

Coliders Don't work 1 Answer

Detect collision of a GO within an instantiated prefab, against my player. 1 Answer


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