Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 bobin115 · Sep 29, 2014 at 09:52 AM · collisionaudiosound

help sound on collision when key down

hi Im brand new to scripting (you can probably tell by my attempt). i was trying to make a script where my player enters a collider and when they press a key it plays a sound. here is my attempt

 var soundFile:AudioClip;
  
 if function OnTriggerEnter(trigger:Collider) {
      if((trigger.collider.tag=="Player")Input.GetKeyDown("f")=true) {
         audio.clip = soundFile;
         audio.Play();
      }
 }

any amendments would be great

thanks

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

Answer by HarshadK · Sep 29, 2014 at 10:01 AM

Attach an AudioSource Component to your game object to which this script is attached and assign your sound file to the Audio Clip from the inspector.

Also your code will become:

 function OnTriggerStay(trigger : Collider) {
      if((trigger.gameObject.tag == "Player") && Input.GetKeyDown(KeyCode.F)) {
         audio.Play();
      }
 }

Here you might want to use OnTriggerStay since you want to check for key press to play the audio source for as long as the player is inside the trigger.

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 bobin115 · Sep 29, 2014 at 10:07 AM 0
Share

it had no error but it didn't play a sound

avatar image HarshadK · Sep 29, 2014 at 10:12 AM 0
Share

Is your sound toggle selected?

Check image below for reference:

alt text

sound.jpg (7.7 kB)
avatar image bobin115 · Sep 29, 2014 at 10:16 AM 0
Share

all i had to do was a little tweak to the script thanks man

avatar image
0

Answer by bubzy · Sep 29, 2014 at 10:00 AM

I would do this :

var collidedWithPlayer :bool = false;

 if function OnTriggerEnter(trigger:Collider)
  {
      if((trigger.collider.tag=="Player")
      {
      collidedWithPlayer = true;
      }
  }
 
 
 function Update() //I use c# so I guess java uses this method for update
 {
 if(collidedWithPlayer && Input.GetKeyDown(KeyCode.F))
 {
    audio.clip = soundFile;
         audio.Play();
 collidedWithPlayer =false;
 }
 }
 


however, this will be a little odd, as the key can be pressed at any time to play the sound after the collision happens, its probably best to just have the sound play on collision, or to set a variable true as I did when the f key is pressed and then play the sound on collision in the collision function.

Comment
Add comment · Show 1 · 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 bobin115 · Sep 29, 2014 at 10:08 AM 0
Share

only 1 error unknown identifier "collidedWithPlayer"

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

26 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

Related Questions

How to emit a sound on specific collision. 1 Answer

How to loop a sound clip on a ButtonDown function 1 Answer

Making a sound using collisions 1 Answer

How to play an audio one shot clip on a collision 1 Answer

Why my sound it's not attaching to my Flashlight 2 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