Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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 Fox_GAMING · May 06, 2018 at 09:14 PM · audiosoundkeypress

How do I play a sound when pressing the W key, and when I stop pressing the W key, the sound stops?

I'm trying to play a sound when a vehicle is moving, so when I press the W key, it moves, and it plays the sound. But as soon as I release the W key, the sound continues to play, it doesn't stop instantly. I'd like for it to play an Idle sound when the vehicle isn't moving, an Shifting sound when W is pressed, and Reversing sound when the car is going backwards (not if S is pressed to slow the car down) and a braking sound when Spacebar is pressed. Plus, for all of these (Minus the Spacebar/brake) I would like it to play an decelerating sound when these keys aren't being pressed. Is this possible? If so, what is the code? How do I fix the W sound?? Please Help!

Script:

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class PlayonW : MonoBehaviour {

 public AudioSource someSound;

 // Use this for initialization
 void Start () {
     
 }
 
 // Update is called once per frame
 void Update () {

     if (Input.GetKeyDown(KeyCode.W)) {
         someSound.Play();
         
         

         if (Input.GetKeyUp(KeyCode.W)) {
             someSound.Stop();
         }

     }
 }

}

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 TanselAltinel · May 07, 2018 at 12:53 PM

You should put your braces correctly for your script to work. Also, don't call Play each frame if the audio source is already playing:

 using System.Collections; 
 using System.Collections.Generic; 
 using UnityEngine;
 
 public class PlayonW : MonoBehaviour {
 
  public AudioSource someSound;
  // Use this for initialization
  void Start () {
      
  }
  
  // Update is called once per frame
  void Update () {
         if (Input.GetKeyDown(KeyCode.W)) {
             if(!someSound.isPlaying) {
                 someSound.Play();
             }
         }
      
         if (Input.GetKeyUp(KeyCode.W)) {
             someSound.Stop();
         }
     }
 }


To your other question: Either have different Audio Sources for your each move set (move forward, reverse, break etc.) and play / stop them like this code, or have different clips and change the clip and see what clip is playing to play stop it at specific key presses.

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 the_monitor · Jan 17, 2021 at 04:57 AM 0
Share

hey, may i ask, if i want every key that i press to make noise. i am currently making a typing game and i want a keyboard typing sound fx. what code should i change

avatar image TanselAltinel the_monitor · Jan 18, 2021 at 06:42 PM 0
Share

I'm not sure what you're trying to achieve, if you want different sound for each key, you can use this and replicate. If you want to use the same sound for all kind of inputs, you can use Input.anyKey.

avatar image
0

Answer by nomanbangash97 · Aug 08, 2021 at 06:15 AM

public AudioSource audioSource;

// Use this for initialization

void Start () { audioSource = GetComponent(); }

// Update is called once per frame

void Update () { if (Input.GetKeyDown(KeyCode.W)) { audioSource.Play(); }

      if (Input.GetKeyUp(KeyCode.W)) 
     {
          audioSource.Stop();
      }
  

}

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

110 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

Related Questions

Audio source playing different soundfiles 0 Answers

Increase the volume of sound source past limit? 1 Answer

GetSpectrumData works only for 3 sec of microphone input 0 Answers

Play sound from an array, not random, and only once 3 Answers

play sound on collecting pickUp Item 3 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