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 Jammer3000 · Mar 08, 2013 at 08:21 PM · c#audio

How to create sound on keyboard button?

Hi I have a game where I want it so when the arrow keys are clicked it emits whatever sound is assigned to the audio source attached to that object. It is a walking sound. And I need it to be in C# please. Thanks

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 ByteSheep · Mar 08, 2013 at 08:27 PM 0
Share

Put something like this into your update function:

 if(Input.GetAxis("Horizontal"))
 {
    audio.Play();
 }

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Hyperion · Mar 08, 2013 at 10:38 PM

The way it's done is you add an Audio component to your object. Choose the correct sound, and when you want it played:

 using UnityEngine;
 using System.Collections;
 
 public class something: MonoBehaviour{
    // setting up the audio component
     audio.loop = true;
     audio.playOnAwake = false;

    if(Input.GetAxis("Horizontal")){   
        audio.Play();
    }
    else
    {
        audio.Stop();
    }
 }

But that's only if you have one Audio component. If you want your character to have multiple sounds, make a variable for the sound...

 AudioClip: yourSound;

Then do

 if(whatever)
 {
 AudioSource.PlayClipAtPoint(yourSound, transform.position);
 }

Remember to drag your sound into the variable slot in the Unity inspector after coding. Although this second option is for non-looping sounds usually, you can fiddle around with it. I suggest using the first one for walking, but the second option for stuff like shooting. Please thumbs up my answer-Hyperion.

Comment
Add comment · Show 7 · 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 Jammer3000 · Mar 09, 2013 at 01:48 AM 0
Share

Thanks so much for the help, but when ever I use the first option it still gives me the error I was getting before.

Assets/AudioTrigger.cs(25,4): error CS0029: Cannot implicitly convert type float' to bool'

avatar image Hyperion · Mar 09, 2013 at 02:28 AM 0
Share

That's really weird, since in my C# script it, the elements work perfectly. Of course, I bunched together multiple aspects that I use in different scripts...

So could you please tell me where line 25, space 4 is (what code it refers to)? That way, we can pin point the problem.

-Hyperion

avatar image syclamoth · Mar 09, 2013 at 02:31 AM 0
Share

It's pretty clearly referring to the bit where you try to use

 Input.GetAxis("Horizontal")

as an if-statement conditional. Try replacing that with

 if($$anonymous$$athf.Abs(Input.GetAxis("Horizontal")) > 0.2f)

(which checks if the axis is over a certain $$anonymous$$imum value) and see how you go.

avatar image Jammer3000 · Mar 09, 2013 at 03:28 AM 0
Share

Yes syclamoth's answer worked thanks, but now when I hold down the left or right arrows to move it just makes a buzzing noise likes its playing the sound really fast or something?

avatar image Hyperion · Mar 09, 2013 at 05:06 AM 0
Share

O.$$anonymous$$. There are two options.

-Option 1: $$anonymous$$ake a variable called walksounding and set it to false. Then, inside the if statement, only play audio if the walksounding variable is false. If it is false, play the sound, then set walksounding to true.

-Option 2: (at least I think that Get$$anonymous$$eyDown only happens once, so...)Change GetAxis("Horizontal") to Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.UpArrow)||Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.DownArrow). If you want to change what keys you press, go to http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$eyCode.html for more key codes.

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

13 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

Related Questions

Audio Play Once 2 Answers

Play music trough scenes 0 Answers

How to make Audio "Range" Further. 2 Answers

Inputing an Equation 0 Answers

How to emit a sound on specific collision. 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