I’ ve been triggering the volume of an audiosource via OncollisionEnter and Exit. Tha audio clips are playing just fine. However, I had to use an OnCollisionStay function to trigger the volume. The audioclips are playing BUT not fluently. you can hear many many small clicks. Is this normal? How can I avoid it?
(the audio files are all .aiff)
here is the script:
function OnCollisionEnter () {
for (var j=0.0;j<1.1;j+=0.1) {
audio.volume = j;
yield WaitForSeconds (0.01);
}
}
/*function OnCollisionStay () {
if (GameObject.Find("Car").GetComponent(Timer).seconds < 5){
for (var i=1.1;i>0.0;i-=0.02) {
audio.volume = i-0.1;
yield WaitForSeconds (0.01);
}
for (var j=0.0;j<1.0;j+=0.05) {
GameObject.Find("Bass").GetComponent(AudioSource).volume = j;
yield WaitForSeconds (0.01);
}
}
}
*/
function OnCollisionExit () {
if (audio.volume > 0.0){
for (var k=1.1;k>0.0;k-=0.1) {
audio.volume = k-0.1;
yield WaitForSeconds (0.01);
}
}
}
What are you trying to do with OnCollisionStay, exactly? That function gets called a lot (60x per second when objects are in contact). You probably shouldn’t use yield statements in OnCollisionStay no matter what you’re doing.
I have a car, which has a Timer script attached to (a countdown).
In the function OnCollisionEnter:
When the car enters a road the volume of that road’s audiosource increases and the timer starts the countdown.
In the function OCollisionStay:
(The car is still on the road)
When the timer drops under 5 seconds, I dicrease the volume of the road’s audiosource and I increase the volume of another object’s audiosource. The loops in the script work like a crossfading process (Loops is the only way I know to fade in/out the audio!)
so, if I remove the yield statements, I’m loosing the crossfading process.
well, I started with StarManta idea. I made this script (OverLapState.js) and attached to each one of the three roads in the game.
var overlap = false;
function OverYes () {
overlap = true;
}
function OverNo () {
overlap = false;
}
then I created this script (Overlapping.js) and I attached it to the car:
(it’s only for when the car is driving on the first road (Plane01))
var lap1;
function Start () {
lap1 = GameObject.Find("Plane01").GetComponent(OverlapState);
}
function OnCollisionEnter (collision : Collision) {
if (collision.gameObject.name == "Plane01")
{
lap1.OverYes();
}
/* else {
lap1.OverNo();
}
*/
}
function OnCollisionExit () {
lap1.OverNo();
}
which it doesn’t work properly. The overlap state of the road (Plane01) turns into true for a second when the car enters the road, and then it goes back into false.
I tried to avoid this by deleting the OnCollisionExit function and writing an else { lap1.OverNo(); } after the “if” of the OnCollisionEnter. It works! but only until the car hits the walls there are beside the road. Then it turns back to false again. Any ideas??
probably because you just entered a collision with the wall so now enter is wall instead of plane01 (though i’m surprised it stays false). i’d try a collider as a trigger as a volume above the road like i said. then use OnTriggerEnter/Exit. make the collider wider than the road so that you can hit the walls without exiting the trigger.
well, I duplicated the road. Placed the duplicant a little above from the road, unchecked the mesh renderer so to be invisible, checked the Is Trigger state of the collider and attached a simple script to see if it works:
function OnTriggerEnter () {
GameObject.Find("Bass").GetComponent(AudioSource).volume = 1.0;
}
function OnTriggerExit() {
GameObject.Find("Bass").GetComponent(AudioSource).volume = 0.0;
}
and it doesn’t!!! very dissappointed, started to messing around and (for no obvious reason) I thought to change the mesh collider of the road’s duplicant with a box collider. I tested it and it seems that it works.
Now I will write the crossfading process in the script and I will do the same with the remaining two roads. I’ll come back later. cross fingers…
it seems to be the same problem as the when I was using the Oncollision function exactly.
I attached the following script to the invisible duplicant of the road in order to add an overlap true/false function:
var overlap = false;
function OverYes () {
overlap = true;
}
function OverNo () {
overlap = false;
}
then I attached the following script to the car:
var lap1;
function Start () {
lap1 = GameObject.Find("Plane01col").GetComponent(OverlapState);
}
function OnTriggerEnter () {
lap1.OverYes();
}
function OnTriggerExit () {
lap1.OverNo();
}
It works very good, BUT at the same time there are a lot of spheres on the 3 roads that the player has to collect while driving. Those spheres have the Is Trigger state as true, so when the car is triggering them the overlap state of the road’s duplicant turns to true as well (same thing as before when the car was colliding with the walls.
Alright, this is the timer.js script from which i adjust the volume of the road’s duplicant’s (Plane01col) audiosource.
var seconds = 11.0;
var timertext : GUIText;
var running = false;
function Update () {
//set the countdown timer and display it
if (running) {
timertext.text = "change track in " + Mathf.Floor(seconds);
seconds -= Time.deltaTime;
}
//reload Level when timer reaches zero
if (seconds < 1) {
Application.LoadLevel ("Level1");
}
//When timer reaches 5, if the car is overlapping Plane01 adjust the volume of Plane01col's audiosource.
if ((seconds < 5) (GameObject.Find("Plane01col").GetComponent(OverlapState).overlap = true;)) {
for (var j=0.0;j<1.1;j+=0.01) {
GameObject.Find("Plane01col").GetComponent(AudioSource).volume = j;
yield WaitForSeconds (0.05);
}
}
}
Is something mispelled(?), cause I got errors and cannot find out what is going on.
Well, I tried to change many thigs on the script but without result.
From the console you can see that there are mistakes in lines 21-24…
I think that the problem is focused in line 21. For a reason, it seems not to understand GameObject.Find(“Plane01col”).GetComponent(OverlapState).overlap = true;, or maybe is the double parenthesis? I really dont know.
not sure if this is it but for booleans in an if statement use == not =
so…
if (… GameObject.Find(“Plane01col”).GetComponent(OverlapState).overlap == true);
yes, I did that, but in order to work I had to remove the ; as well…
the script seems to be correct, only that I have an other problem now. The console says that Update() cannot be a coroutine.
Haha!!
:x
so, I removed the yield. the volume goes straight to 1.0 without a fadein effect AND I have also the annoying clicks, which basicaly means that the loop also doesn’t work well in the Update() ( which makes sense). I removed the loop and the volume goes instantly to 1 BUT without the clicks. So, the question is, how can I have a fade in in a situation like this??
ah yeah, i really didn’t read the script (even copy pasted the; without thinking!). update and fixedupdate are called every frame. so you can’t tell them to wait (yield) x seconds. instead make a function (coroutine).
function Update ()
{
if ((seconds < 5) (GameObject.Find("Plane01col").GetComponent(OverlapState).overlap == true))
{
fadeVolume ();
}
}
function fadeVolume()
{
for (var j=0.0;j<1.1;j+=0.01) {
GameObject.Find("Plane01col").GetComponent(AudioSource).volume = j;
yield WaitForSeconds (0.05);
}
well, well, well… it’s not working very good…for an unknown reason there is something that retracksthe volume back to zero at the same time when the script tries to increase it. As a result I have something like a bad radio signal(!)…
Anyway, i thought another way, not so complicated of doing it. simple, I will make a coroutine that increases the volume slowly in a time period of 10 seconds.
function OnTriggerEnter () {
for (var j=0.0;j<1.0;j+=0.01) {
audio.volume = j;
yield WaitForSeconds (0.1);
}
}
function OnTriggerExit () {
for (var k=1.0;k>0.0;k-=0.1) {
audio.volume = k-0.1;
yield WaitForSeconds (0.01);
}
}
the fade in works great that way, but the problem is that OnTriggerExit there is a fadeout only if the volume has reached the maximum in the fadein process (ontriggerenter). If I have a TriggerExit before the fadein of the TriggerEnter finishes, then I don’t have a fade out.
Is there a way to stop the fadein OnTriggerExit before the fadeout begins???