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 grimdar · Sep 10, 2014 at 06:14 AM · triggersoundkey

Sound on keypress, not working

No Javascript experience here so bare with me. I have a spacecraft, and need it to make an acceleration sound everytime it accelerates. The accelerate key is "z", but I want the sound to stop when you let go of the "z" key.

So here's the code I have but it's not working, there is no sound whatsoever:

     var sound : AudioClip;
      
     function Update()
     {
      
     if (Input.GetKeyDown ("z"))
     {
     audio.PlayOneShot(sound);
     }
      
     else
     {
     audio.PlayOneShot(sound);
     audio.Pause();
     }
      
     }

I attached an Audio Source to a empty game object, then applied the script to the empty game object with the correct sound specified. One other issue is the spacecraft can also be controlled by a joystick. The acceleration key is "joystick button 1", how can I trigger that sound by both the keypress AND joystick button? Any help would be very much appreciated.

Comment
Add comment · Show 1
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 glantucan · Sep 10, 2014 at 06:43 AM 0
Share

Are you asigning your sound var to the AudioClip in the empty game object you mention? You should have something like:

 function Start() 
 {
     var soundGO = GameObject.Find("NameOfSoundGameObject");       
     sound = soundGO.GetComponent("AudioSource").AudioClip;
 }


in your script.

2 Replies

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

Answer by zharik86 · Sep 10, 2014 at 06:54 AM

The function GetKeyDown() works only once after button is pessing. Besides in your condition you always stop audio playback by the function Pause(). I will write a small script:

  var sound : AudioClip;
  private var myAP: boolean;

  function Start() {
   myAP = false;
  }
 
  function Update() {
   //Change function GetKeyDown() on GetKey()
   if (Input.GetKey (KeyCode.Z) || Input.GetKey(KeyCode.JoystickButton1)) {
    if(!myAP) {
     myAP = true;
     audio.PlayOneShot(sound, 1.0);
    }
   } else {
    myAP = false;
    audio.Pause();
   }
  }

I hope that it will help you.

Comment
Add comment · Show 2 · 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 grimdar · Sep 10, 2014 at 04:46 PM 0
Share

Thanks very much! This works very nicely and there's no weird sound issues/popping, however I need the sound to stop once the key is not pressed. Is this possible?

avatar image grimdar · Sep 10, 2014 at 05:20 PM 0
Share

Update: Works perfectly now, I simply changed audio.Pause(); to audio.Stop();

full code

      var sound : AudioClip;
     private var myAP: boolean;
      
     function Start() {
     myAP = false;
     }
      
     function Update() {
     //Change function Get$$anonymous$$eyDown() on Get$$anonymous$$ey()
     if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.Z) || Input.Get$$anonymous$$ey($$anonymous$$eyCode.JoystickButton1)) {
     if(!myAP) {
     myAP = true;
     audio.PlayOneShot(sound, 1.0);
     }
     } else {
     myAP = false;
     audio.Stop();
     }
     }

Thanks for your help.

avatar image
0

Answer by 767_2 · Sep 10, 2014 at 08:43 AM

Update() executes every frame so your sound plays but on the next frame it will pause because you just set it like that audio.Pause(); so that's how you never here anything

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

audio trigger keydown 1 Answer

How to make 2D object trigger a sound once each time it passes 2D player in 3D game?,I want to play a sound once each time passes near my player 0 Answers

Trigger sound when pressing button 2 Answers

How to make a object what can be picked up and when clicked on another object it ends the game? 0 Answers

Trigger Sound to Play Globally Not on Triggered Object 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