- Home /
Can only get collide trigger to work once
I am very new to programming. I am working on animating a door to open when the trigger is hit, and close when the trigger is exited. Here is my code.
var door : GameObject;
function OnTriggerEnter (collision : Collider) { if (collision.gameObject.tag == "Player") { door.animation.Play ("Bouncing Box"); } }
function OnTriggerExit (collision : Collider) { if (collision.gameObject.tag == "Player") { door.animation["Bouncing Box"].speed = -1.0; } }
Problem is it only works the first time. Is there a reset I have to put somewhere so it works everytime I hit the trigger?
Thank you!
Alright, I realized I have the clip wrapmode set to Clamp Forever. If I put it to once, the door opens, but then seems to be frozen. I guess I really just need help writing this script properly. After reading for 3 hours, I haven't found a straightforward answer for a newbie like me. Thanks!
Answer by alexnode · Apr 05, 2010 at 03:43 PM
I think you have to put a boolean to say if the door is open ....do that if the door is not open...open it
private var doorIsOpen : boolean = false; var door : GameObject;
function OnTriggerEnter (collision : Collider)
{
if ((collision.gameObject.tag == "Player") && (doorIsOpen = false))
{
door.animation.Play ("Bouncing Box");
doorIsOpen = true;
}
}
function OnTriggerExit (collision : Collider)
{
if ((collision.gameObject.tag == "Player") && (doorIsOpen = true))
{
door.animation["Bouncing Box"].speed = -1.0;
doorIsOpen = false;
}
I am not a programmer but after finishing a few tutorials I have this working code that works for me. So.. hope it works. With a few alterations it can work onCollider hit too.
If you are a modeller that wants to have a system that works with every door tagged as "doorTrigger", I suggest whenever you import an animated door model, do the following In the 3dPackage A: Create two animations one opens, one closes.(I haven't tried to inverse the animation but it sound a good idea) For example frame 0 to frame 50 opens, 50 to 100 closes.
B: If your trigger has to be very precise in shape (quite rare ) do it in the 3d package and name it trigger something. I usually add a cube as a trigger in the 3d package but you can add them in unity too as a child of the root gameObject.
In unity drag your model in your scene.
A:add the script at the end of the answer in your game First Person Controller or whatever character controller you have. B: When you import the model add the animations in the root Game object. (an option in the import settings). This way you play all your animations for your model by finding your root game object. Use the same name for all your different doors or windows etc( this way whenever you hit a trigger ... called let's say doorOpen you always play an animation called "dooropen" even for different doors or a window etc... C: Give your trigger a tag like "doorTrigger" and check the isTrigger box in the mesh collider. D: (optional)Add sound for the door by adding a sound source to your first person controller or other controller and assign the audio clips under your attached script.
private var doorIsOpen : boolean = false; private var timer : float = 0.0; private var doorObject : GameObject;
var doorOpenSound : AudioClip;
var doorShutSound : AudioClip;
function OnTriggerEnter (hit:Collider)
{
if((hit.gameObject.tag == "doorTrigger") && (doorIsOpen == false)){
doorObject=hit.gameObject ;
doors (doorOpenSound,"dooropen", doorObject,true);
}
}
function Update()
{
if(doorIsOpen)
//if you activated the trigger you might want the door to close after a while.
// you can close your door on trigger exit instead ... just make sure that the animation //is completed before the door closes
{ timer += Time.deltaTime; }
if(timer >= 6)
{
doors(doorShutSound, "doorshuts", doorObject, false);
timer = 0.0;
}
}
function doors (aClip: AudioClip ,animName : String, doorObject :GameObject, doorCheck : boolean)
//here you create a function that combines all of the necessary elements for the //animation
// your audio clip , the name of the animation you want to play, your gameObject, and //boolean if the door is open or close.
{
audio.PlayOneShot(aClip);
doorObject.transform.parent.animation.Play(animName);
//this way you play the animation stored in the root object of your model
doorIsOpen = doorCheck;
}
@script RequireComponent(AudioSource)
Thank you so much. I will give it a shot. I really appreciate your detailed help!
Trying out your code, but I think there may be a few typos. Getting a lot of errors.
I think that the code works ... the second one. make sure you drag the script in your first person controller, add an audio source to the first person controller, in the script in the hierarchy panel under the first person controller define the audio clips. make sure that the name of the animation exists on your parent object.$$anonymous$$ake a trigger with a tag "doorTrigger" (ticked that as trigger in the collision properties). This trigger has to be below your root object that holds the animation. Store your animation in your root object and have your trigger as a child. what else?
Answer by qJake · Apr 05, 2010 at 01:36 AM
To be honest, Animations aren't suited for such simple tasks as opening and closing doors. You may find it easier to just script the door, using Coroutines or something similar. Just "Transform" the door upwards and downwards when the player enters and exits the trigger.
I can't provide more detailed help, because it sounds like it's a problem with the way you've animated the door, and I can't see your animation data.
Sure I get that, but in this particular case, the door has an elaborate animation when opened and closed, similar to a bank vault spinning open. So I think I have to make the animation work somehow.
Thank you.
Oh, I wasn't aware it was a complex door... I thought it was just a simple sliding door. Then yes, you'll need to work something out with your animations (however they're done).
Answer by ric_eng · Apr 08, 2010 at 01:07 PM
Hi, I have a similar problem. I made an animation in 3dsmax and put the settings open door frame 30 to 0. When I try to activate the reverse mode(speed = -1)it will not start. Is there any way to make the model back to the starting position, as if the animation did not occur. I tried using rewind but did not work. I just want to close the door after opening animation