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 07, 2013 at 02:25 PM · audioaxis

Play audio on keyboard click?

Hi I have a simple script that plays different audio clips on various button or mouse clicks, now I have the code setup to play an audio clip that is assigned in a public variable on left mouse click, but I need it to do that and also play the audio that is assigned to the audio source when I click the up and down arrow keys, I thought I had it figured out, but it keeps giving me the error/errors below. Also Please give me the code in C# thanks.

Error: Assets/AudioTrigger.cs(21,17): error CS0029: Cannot implicitly convert type float' to bool'

Here's the code I'm using: using UnityEngine; using System.Collections;

[RequireComponent(typeof(AudioSource))] public class AudioTrigger : MonoBehaviour { public AudioClip swing; // Use this for initialization void Start () {

 }
         
 // Update is called once per frame
 void Update () 
 {
             
     if(Input.GetButtonDown("Fire1"))
         audio.PlayOneShot(swing);

     if(Input.GetAxis("Vertical"))
         audio.Play();
 }

}

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Landern · Mar 07, 2013 at 02:32 PM

Your problem is with Input.GetAxis which returns a float.

From the documentation:

The value will be in the range -1...1 for keyboard and joystick input. If the axis is setup to be delta mouse movement, the mouse delta is multiplied by the axis sensitivity and the range is not -1...1.

The should look like:

 if(Input.GetAxis("Vertical") > 0)
   audio.Play();
 
 // or
 
 if(Input.GetAxis("Vertical") < 0)
   audio.Play();
 


Comment
Add comment · Show 4 · 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 07, 2013 at 03:04 PM 0
Share

Ok That worked, but now it rapidly plays the audio clip so it just sounds like buzzing. What I'm trying to do by the way is have it so when I click the up or down arrows on the keyboard it plays my footsteps sound in the audio source.

avatar image Polinator · Mar 07, 2013 at 03:51 PM 0
Share

You can't oneshot the audio and then loop it?

Or make it so when it plays, have a bool "audioPlaying = true" and only play when it's false. And then have "audioPlaying = false" when the audio stops, or when GetAxis("Vertical") = 0

avatar image Jammer3000 · Mar 07, 2013 at 04:10 PM 0
Share

I am sorry, but I'm new to program$$anonymous$$g is there anyway you could show me what your talking about or something else?

avatar image Mander · Mar 07, 2013 at 05:02 PM 0
Share
  if (!audio.isPlaying)
     {
     if(Input.GetAxis("Vertical") > 0)
        audio.Play();
         
     }

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

How to create sound on keyboard button? 1 Answer

How to play loop an audio clip on button hold down? 3 Answers

Object won't rotate z axis on second click? 2 Answers

How to emit an audioClip on Collision 0 Answers

help sound on collision when key down 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