• 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
Question by theFTWnetwork · Dec 01, 2012 at 09:04 PM · javascripttimesound

Breathing sound effect using timers

Hello, i'm trying to make a breathing sound effect in javascript, and i think timers would be the best. What i had in mind for the script to do was this: If you sprint for 6 seconds (shift + w and shift + arrow up), play a breathing sound. After walking for 8 seconds, it can be repeated, else not. I have no idea on how to do this script, so every help would be great!

Okay, what i tried is the cod below, but it isnt working, can anyone tell me whats wrong?

var breath : int = 6; var sound : AudioClip;

function Update() { if (Input.GetKey(KeyCode.LeftShift)){ if (Input.GetKey(KeyCode.W)){ Invoke("gosoung", breath); Debug.Log("Started"); } Debug.Log("Cancelled"); CancelInvoke("gosound"); } }

function gosound(){ audio.clip = sound; audio.Play(); Debug.Log("Playing"); }

Comment
aldonaletto
Fattie

People who like this

0 Show 4
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 Fattie · Dec 01, 2012 at 09:04 PM 0
Share

this is incredibly simple. you just use Invoke()

go to http://unityGEMS.com for a massive beginner intro on timers using Invoke()

avatar image theFTWnetwork · Dec 01, 2012 at 11:55 PM 0
Share

Okay, i've looked over the site, but didnt find anything about Invoke, can you give me a small script that i can build on?

avatar image PaulBrasfield · Dec 02, 2012 at 12:15 AM 1
Share

We don't write the scripts for you, stop being lazy and write it yourself. We aren't your slaves.

avatar image theFTWnetwork · Dec 02, 2012 at 12:25 AM 0
Share

a SMALL example of how i could do it, it can be 3 lines of code, i dont need the entire script!

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by aldonaletto · Dec 02, 2012 at 12:33 AM

As @Fattie said, InvokeRepeating is the best solution for fixed rate actions - but in this case it could complicate things because the rate must be variable. If you want to breathe according to the character speed, something like this could be more suitable (attach this script to the character):

 var breathSound: AudioClip; // assign the breath sound in the Inspector
 var walkRate: float = 4.0; // breath interval when walking
 var walkSpeed: float = 1.5; // is walking when above this speed
 var runRate: float = 2.0; // breath interval when running
 var runSpeed: float = 3.5; // is running when above this speed
 
 private var lastPos: Vector3;
 private var nextBreath: float = 0;
 private var interval: float = 0; // breath interval - 0 means no breath sound
 
 function Update(){
   var dt = Time.deltaTime;
   if (dt > 0){ // only breathe when game not paused!
     // calculate the distance since last frame:
     var dist = Vector3.Distance(lastPos, transform.position);
     // update lastPos:
     lastPos = transform.position;
     // calculate the current speed:
     var speed = dist/dt;
     if (speed > runSpeed){ // is running?
       interval = runRate;
     }
     else
     if (speed > walkSpeed){ // else is walking?
       interval = walkRate;
     }
     else { // else stop breath sound
       interval = 0;
     }
     // if breath enabled and it's time to breath...
     if (interval > 0 && Time.time > nextBreath){
       audio.PlayOneShot(breathSound); // play the sound...
       nextBreath = Time.time + interval; // and set next time to breath
     }
   }
 }
Comment

People who like this

0 Show 3 · 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 theFTWnetwork · Dec 02, 2012 at 01:50 AM 0
Share

I added a script i tried to my first question, can you telll me why it isnt working?

avatar image aldonaletto · Dec 02, 2012 at 02:28 PM 0
Share

You're starting the breath sound when shift and W are pressed, but immediately after you're cancelling the invoke.You're also using Invoke, which will only play the sound once after the breath interval. There's also a typo in Invoke: you're calling "gosoung" instead of "gosound". The code should be like this:

 ...
 private var breathing = false;

 function Update() { 
   if (Input.GetKey(KeyCode.LeftShift) && Input.GetKey(KeyCode.W)){
     if (!breathing){ // only start Invoke once
       InvokeRepeating("gosound", breath, breath);
       breathing = true;
     }
   } else if (breathing){ // no shift+W, cancel invoke (if any)
     CancelInvoke("gosound");
     breathing = false;
   }
 }
 ...
avatar image theFTWnetwork · Dec 02, 2012 at 07:19 PM 0
Share

It only needs to play it every 6 seconds, just 1 time, willthat code do it?

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

12 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

Related Questions

Finish One Combo 1 Answer

Realtime Clock? (needles) 1 Answer

Keeping track of time? 4 Answers

How to save and display players times using Playerprefs in Javascript? 1 Answer

A node in a childnode? 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