• 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 fragdady · Oct 27, 2013 at 06:01 AM · c#audiosoundwaitforseconds

play audio for few seconds on key hit

Hello all. Not much of a coder but I'm trying to have a short audio clip play whenever the player presses spacebar.

Right now the player is able to jump by pressing spacebar and if they hold it down they continue to rise for about three seconds before they begin falling. I would like the audioclip to play for as long as they are rising, but currently it plays the entire audio file (about 27 seconds long). I understand that my current code doesn't tell it to stop, but I'm having a hard time figuring out how to play the audio, wait for three seconds, then stop and reset the audio.

my spacethrusters script (C#) is attached to my fps controller along with the audiosource component.

Any help at all would be great, here's the current code I have:

using UnityEngine; using System.Collections;

public class spacethrusters : MonoBehaviour {

 //Update is called once per frame
 void Update () {
     if (Input.GetKeyDown("space"))
         audio.Play ();
     }

}

Thanks again.

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 Zamaroth · Oct 27, 2013 at 10:50 AM

I suppose you have a variable, used to determine if the player is falling or not. If this variable is called falling, the code to stop the audioclip looks like this:

 if(Input.GetKeyUp("space") || (Input.GetKey("space") && falling == true && !audio.isPlaying))
 {
     audio.Stop();
 }

First statement Input.GetKeyUp("space") is used to stop the audioclip when player stops holding the spacebar.

Second statement Input.GetKey("space") && falling == true stops the audioclip when player begin falling, but still holds the spacebar.

!audio.isPlaying is there to ensure that the function won`t be called if the audioclip has been already stopped.

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 fragdady · Oct 27, 2013 at 03:17 PM 0
Share

That looks perfect. How would I go about creating the 'falling' variable? I looked into some raycasting options but I got a bunch of errors. Sorry for my inexperience.

avatar image Zamaroth · Oct 27, 2013 at 08:33 PM 0
Share

Right now the player is able to jump by pressing spacebar and if they hold it down they continue to rise for about three seconds before they begin falling.

Simply create a boolean variable named falling

 var falling : boolean;

and set it to true after those 3 seconds.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

16 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

Related Questions

Multiple Cars not working 1 Answer

someone is there? 1 Answer

Audio listener to mono then to left or right speaker? 2 Answers

Sound on collision not working 1 Answer

Get acess and switch to many audio source in a single Object HELP!! 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges