hey guys i have a question whats wrong with this script
i want to get it so that when i walk into a box witch is a trigger assinged is trigger.
that if i press f when i am inside the trigger the audio will play
I believe the problem is that OnTriggerEnter() only gets called when you actually pass through the bounds of the trigger collider. If you're standing inside the collider, it won't be called.
Instead, I would have a boolean variable insideTrigger, which is false by default, and set to true inside OnTriggerEnter() (and false inside OnTriggerExit() ). Then you just have your F key detection also check for insideTrigger being true, and put it in Update() instead of OnTriggerEnter().
OnTriggerEnter is only sent on the frame in which the trigger is, well, entered. Further, Input is typically checked in the Update () method. So the ideal thing to do is set a flag whenever something is inside the trigger and, in Update, check for both "f" being pressed and the flag being true. You might use a boolean for the flag, but you can also make sure that the thing leaving the trigger area is also the thing that entered it by keeping a reference to some GameObject and seeing if it's still there (by testing against null).
var inside : GameObject;
function OnTriggerEnter (other : Collider) {
inside = other.gameObject;
}
function OnTriggerExit (other : Collider) {
// If whatever is leaving is the same thing that entered, stop keeping track of it.
if (other.gameObject = inside) {
inside = null;
}
}
function Update ()
{
// Check both conditions
// I used "GetKeyDown" here instead of "GetKey" because I'm guessing you don't want the audio to start playing every frame the key is down. If that's not the case, go ahead and change it back.
if (inside != null && Input.GetKeyDown ("f")) {
audio.Play (03);
}
}
I changed it to GetKeyDown which will call play on the frame the key goes down after they enter the collider. Using GetKey, it will play (restart?) each frame unless you also check the audio.isPlaying (or something like that).