Hi, first of all, thanks to all the people that write in these forums, I’ve been looking into unity for a few weeks and this is one of the best places for interesting finds of all sorts… anyway, I’m very new to coding in general, so please be patient :
I’m searching a way to have a room with 4 (or more) zones in it, each attached to a specific sound.
The 4 sounds are exactly the same length (2 bar - same tempo - beat tracks, for example)
I want the sound of the zone where the camera(FPS)is to be the only one heard and when the camera moves into a new zone the new sound loop starts and the old one stops.
To make sure the loops are synchronized, I thought about playing all sound files at the startup and then controlling the different sound volumes with triggers in each zone, but after a few days searching around I’ve come to nothing
I’m not even sure where the scripts and the sounds should be (in the main camera, or in each trigger) and I also only want it to react with the main camera, not with other objects that may get thrown around in the room.
I would also like to be able to have overlapping zones each with there own sound file.
Ok, I’m still searching, and on my way to it, I fell on this problem that seems very weird to me.
This .js code is attached to a big trigger cube. When something enters into the trigger, a sound should be played in a loop, if no one or nothing is left in the space the sound should turn off.
Here is my simple code:
public var isColliding = false;
function OnTriggerStay() {
isColliding = true;
}
function OnTriggerExit() {
isColliding = false;
}
function Update() {
if (isColliding==true) {
audio.Play();
}
}
But for some reason, it’s back to front, it wait’s for a triggerExit message to start the sound, and stops it when there is triggerStay…
Could someone just give me a hint for this last problem. Even if it’s just a simple phrase like “No you can’t do such a thing” or if I need to write part of the code upside down to make it work.
My project is nearly finished and with this version of the script, I have a few hundred objects working back to front???
The collisions seems to work well, because my variable (isColliding) acts like it should (I can see the check box go on and off)
it must be as you say something to do with the update fonction. I tried your snippit of code, but for some reason there is no sound at all when you add the line
If the colliders are working as they should (I assume you added a debug.log(“collider active”) to your OnTriggerStay() func, etc etc), then your isColliding var should have the correct effect on your sound files.
If you can truly rule that out, then the problem would have to be with your sound files, though you’ve probably gone through that those already.
I suspect there’s some minor syntax error or something, or this’d work fine.
I know what I’m saying doesn’t seem to agree, that’s what makes it so puzzling for me. I put debug.log messages after each step, the collider side works fine, and It’s not the sound file for any reason, because it works fine (but inverted). So it must be something to do with the syntax or maybe the idea is back to front, but it seems quite logic to me.
I’d hazard a guess that the problem might be you’re calling “Play” every update cycle? So it’s resetting the sample back to the start of the track, then as soon as someone leaves the collider it’s free to play the rest of the audio.
I think you are right, but actually, with your code it now does what you thought it was doing before (it’s upside down again). It’s looping the beginning of the sound by tiny little audio snipits. (with FixedUpdate or by frame Update
I saw some code somewhere around this forum about a good way to loop audio files (by setting it’s length and doing one-shots). I’m sure that your code and this may get something working. (But I’m not sure about anything anymore)