- Home /
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"); }
this is incredibly simple. you just use Invoke()
go to http://unityGE$$anonymous$$S.com for a massive beginner intro on timers using Invoke()
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?
We don't write the scripts for you, stop being lazy and write it yourself. We aren't your slaves.
a S$$anonymous$$ALL example of how i could do it, it can be 3 lines of code, i dont need the entire script!
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
}
}
}
I added a script i tried to my first question, can you telll me why it isnt working?
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" ins$$anonymous$$d of "gosound". The code should be like this:
...
private var breathing = false;
function Update() {
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.LeftShift) && Input.Get$$anonymous$$ey($$anonymous$$eyCode.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;
}
}
...
It only needs to play it every 6 seconds, just 1 time, willthat code do it?
Your answer
Follow this Question
Related Questions
How to save and display players times using Playerprefs in Javascript? 1 Answer
Need help with this Rotation Script (fixes) 1 Answer
A node in a childnode? 1 Answer
Finish One Combo 1 Answer
Realtime Clock? (needles) 1 Answer