- Home /
Timer inexplicably counting down slower after it reaches a certain number.
Hi there, I recently encountered a problem that I can't seem to even begin figuring out, so any help would be greatly appreciated.
I have a script specifically for the breathing sound effects of a running character, and I have a timer that uses Time.deltaTime to determine the volume of the effects based on how long the character has been running. When the character can no longer run, another script stops that action and in this script the volume timer begins counting back down.
All of this works perfectly and just how I expect, except when the timer reaches 9 (which is where I've capped it). When this happens, the timer starts counting down agonizingly slow instead of it's normal rate.
EDIT: It just occurred to me that it might be easier for everyone if I pointed out what variable is playing up here. volumeTimer is the one.
Here's the script:
#pragma strict
@script RequireComponent(AudioSource)
var breath1 : AudioClip;
var breath2 : AudioClip;
var breath3 : AudioClip;
var breath4 : AudioClip;
var breath5 : AudioClip;
var breath6 : AudioClip;
var breath7 : AudioClip;
var breath8 : AudioClip;
var breathEnd : AudioClip;
var timer = 0f;
var whichBreath : int;
var volume = float;
var volumeTimer = 0f;
var outOfBreathTriggered = false;
private var devMvmnt : DevMovement;
function Start () {
whichBreath = Random.Range(1,8);
devMvmnt = transform.root.GetComponent(DevMovement); //Fetches the sprinting mechanics to stop heavy breathing when sprinting is no longer available.
}
function Update () {
if (Input.GetKey("w") && Input.GetKey("left shift") && devMvmnt.sprintTimer < 9){
timer += Time.deltaTime;
volumeTimer += Time.deltaTime;
audio.volume = volumeTimer / 60; //Sets the volume for the breathing sound effect. Longer you run, the louder it gets.
if (whichBreath == 1 && timer >= .7){
audio.clip = breath1;
audio.Play();
}
else if (whichBreath == 2 && timer >= .7){
audio.clip = breath2;
audio.Play();
}
else if (whichBreath == 3 && timer >= .7){
audio.clip = breath3;
audio.Play();
}
else if (whichBreath == 4 && timer >= .7){
audio.clip = breath4;
audio.Play();
}
else if (whichBreath == 5 && timer >= .7){
audio.clip = breath5;
audio.Play();
}
else if (whichBreath == 6 && timer >= .7){
audio.clip = breath6;
audio.Play();
}
else if (whichBreath == 7 && timer >= .7){
audio.clip = breath7;
audio.Play();
}
else if (whichBreath == 8 && timer >= .7){
audio.clip = breath8;
audio.Play();
}
if (timer >= 0.7){
timer = 0.0;
whichBreath = Random.Range(1,8);
}
}
if (volumeTimer >= 9 || audio.volume >= .2){
volumeTimer = 9;
audio.volume = .2; //Sets the maximum volume.
}
if (devMvmnt.sprintTimer >= 9 && outOfBreathTriggered == false){ //Plays the ending sound for catching the breath when you can't run anymore.
audio.clip = breathEnd;
audio.Play();
outOfBreathTriggered = true;
}
if (devMvmnt.sprintTimer <= 6){
outOfBreathTriggered = false;
}
if (!Input.GetKey("left shift")){ //Reduces the breathing volume over the time you're not sprinting.
timer = 0.0;
volumeTimer -= Time.deltaTime * 1.5;
if (volumeTimer <= 0){
volumeTimer = 0;
}
}
}
I'm pretty new to all of this, so thanks for the info. I'll look into it.
Your answer
Follow this Question
Related Questions
Play Audio at the end of a Countdown 0 Answers
How can I play a sound in sync with a countdown? 1 Answer
C# countdown timer 9 Answers
Display countdown timer in Unity: distance traveled C# 1 Answer
Countdown 2 Answers