Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
2
Question by LEMONVirus99 · Dec 13, 2012 at 06:09 PM · colliderplayersound

How to activate a sound when a player walks through a collider

I have an area that when a player enters the collider it will play a sound. Because im new to unity i have no idea how to code so what would i put and how would i make the sound only go off once?

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
3
Best Answer

Answer by clunk47 · Dec 13, 2012 at 06:54 PM

To have the audio only play once, use PlayOneShot(clip, volume) I have an example below. Volume ranges from 0.0 to 1.0, which makes is a float, so you use 0.0f to 1.0f. This is a C# example btw. You may also only want the audio to play if it is not already playing, so you don't get a bunch of the same sound playing over itself. To do this, you would use if(!audio.isPlaying) the "!" means "not". You could use `if(!audio.isPlaying) audio.Play();` and it will still only play once per collision, if it's not already playing. This might not make sense if you don't know how to code at all, take a look at the script reference for AUDIO for more information.

If the area is a TRIGGER:

 void OnTriggerEnter(Collider coll)
 {
     if(coll.gameObject.Tag == "Player")
         audio.PlayOneShot(audio.clip, 1.0f);
 }
 

If the area is NOT a Trigger, but a physical collider:

 void OnCollisionEnter(Collision coll)
 {
     if(coll.gameObject.tag == "Player")
         audio.PlayOneShot(audio.clip, 1.0f);
 }

 OR you can just use Play(); But first, make sure audio is not already playing. If you want this script to only work once, Destroy it after trigger.

 if(coll.gameObject.tag == "Player")
 {
     if(!audio.isPlaying)
     {
         audio.Play();
         Destroy(this);
     }
 }


Comment
Add comment · Show 13 · 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 clunk47 · Dec 13, 2012 at 08:22 PM 2
Share

Because you didn't define a class. $$anonymous$$y example wasn't just for copy and paste lol. This is C#, you need to know a bit of C# to do it.

 using UnityEngine;
 using System.Collections;
 
 public class audioTriggerTest : $$anonymous$$onoBehaviour
 {
     void OnTriggerEnter(Collider coll)
     {
         if(coll.gameObject.tag == "Player")
             audio.PlayOneShot(audio.clip, 1.0f);
     }
 }
avatar image clunk47 · Dec 13, 2012 at 08:26 PM 2
Share

Note the class name "audioTriggerTest", you can name whatever you want, then you must name the script file the same as your class or it won't work. Note this C# Script derives from $$anonymous$$onoBehaviour.

avatar image LEMONVirus99 · Dec 13, 2012 at 08:34 PM 2
Share

oh sorry, my bad i know nothing of C# just a bit of java. This may sound stupid but it says that the mono script is missing, what exactly is this? And also it says that 'Type 'UnityEngine.GameObject' does not contain a definition fo 'Tag' and no extension method 'Tag' of type 'UnityEngine.GameObject' could not be found' thanks, i appreciate your help!

avatar image clunk47 · Dec 13, 2012 at 08:55 PM 3
Share

That's what PlayOneShot means. Take a look at the script ref I posted in my original answer. Well your original question isn't very specific... Do you want the audio to play only once, period? Or each time the player interacts? If only ONCE throught the whole level, Destroy the script after it has been used.

     if(coll.gameObject.tag == "Player")
     {
         if(!audio.isPlaying)
         {
             audio.Play();
             Destroy(this);
         }
     }

Notice I didn't use PlayOneShot this time, I just checked if audio was playing. "!" means not. So this declares that if the audio is NOT playing, then Play it. Then destroy the script. You can also replace Play(); with PlayOneShot(audio.clip, 1.0f);, but I don't know exactly what you want. Experiment around with and without destroying the script, Play and PlayOneShot, checking if audio is already playing, etc....

avatar image clunk47 · Dec 13, 2012 at 09:07 PM 3
Share

Thank you Sir, very happy I could help you out :)

Show more comments

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

10 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

Related Questions

Restart sound when re-entering trigger? 2 Answers

How to handle multiple colliders for one enemy and bullet. 3 Answers

Detecting when Player stoped to push a rigidbody? 3 Answers

Rigidbody Character Jumps Higher With At Least 2 Collisions 3 Answers

Moving colliding Object causes Player to jitter 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