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
0
Question by UnrealIzzy · Aug 04, 2012 at 07:39 AM · audiotriggerclickplayleft

Need help on audio trigger.

Hi Guys, I am making a script to attach to my "is Trigger" Box Collider. but I am a bit stuck, I can't seem to get the audio to play when my character clicks his left mouse button while he is in the trigger zone???

 var Trigger : AudioClip;
 
 function OnTriggerEnter(){                  //when he enters the trigger zone
      if (Input.GetButtonDown ("Fire1")) {   //if he presses the Fire1 Button
      audio.Play();                          //audio plays
     }
 }

I wan't the character to be able to "Left Click" and play the audio inside of the trigger zone??? Thanks :D

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

2 Replies

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

Answer by DannyB · Aug 04, 2012 at 07:52 AM

Let's eliminate problems first and handle the audio only.

Put a Debug.Log("here") inside tho IF condition, if you are not getting to it, the problem is not the audio.

If you are getting to it, and it just wont play (but with no error), then it is probably the settings of your Audio Source component. Make sure it is configured to be heard at a long distance (or at least at the distance you want it to be heard) by playing with the properties in he 3D Sound Settings area.

Comment
Add comment · Show 3 · 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 UnrealIzzy · Aug 04, 2012 at 08:16 AM 0
Share

Thanks Alot!

I added the debug.Log and found it wasn't working properly so I changed the function from " function OnTriggerEnter" to " function OnTriggerStay " This way the player can activate it if he stays within the zone :D

Again Thanks =D

avatar image DannyB · Aug 04, 2012 at 08:29 AM 0
Share

Excellent. I would also like to suggest a slightly modified approach, that adds a little more separation and control to your code.

OnTriggerEnter, set a variable canFire=true;

OnTriggerExit, set it to false.

Then, in your Update(): if( Input.GetButtonDown( "Fire1" ) && canFire ) audio.Play()

avatar image UnrealIzzy · Aug 04, 2012 at 08:49 AM 0
Share

Thanks for the suggestion works perfectly :D

avatar image
0

Answer by UnrealIzzy · Aug 04, 2012 at 08:16 AM

Final Code:

 var Trigger : AudioClip;
 
 function OnTriggerStay(){
      if (Input.GetButtonDown ("Fire1")) {
      audio.Play();
      Debug.Log("audio is working");
      }
 }
Comment
Add comment · 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

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

8 People are following this question.

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

Related Questions

Play Audio Sound OnTriggerEnter 2 Answers

Stream multiple audio files 1 Answer

Can't click gameobject when over another trigger? 1 Answer

Multiple Audio Sources On One Object 0 Answers

I wanna know how to click on something and have it play a audio 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