- Home /
Begginers question, Simplifing a script, opening two doors with 2 overlapping triggers.
I have the script below that works but it seems too big and unnecessary for the task. I have two rotating "sliding doors" that play with animation from .max file. I don't want to combine the doors to one because they use different materials that are repeated all over the project. I put two overlapping triggers to open them when you approach with the default first person controller.
The triggers are children of the parent animated object because I though that searching by tag or name will be slower. They play the animation with "gameObject.transform.parent...." is that a correct approach ? If I use one trigger I'll have to find the objects by tag and when you have many instances of these tags it seems that things are becoming a bit random on what door is opening. When I do the artwork (yes I am one of those ) I put an empty box called triggerXXX in my model and if I have multiple animations in one file I put multiple triggers as children. I do that because it is faster than snapping in unity and because I don't want to brake the connection with the original models which fills me with metaphysical fear !!! ... of assigning all the layers,the tags, the triggers and the materials again in a later stage. So as a strategy what is the most elegant way of assigning triggers that can work on multiple different objects?
//I duplicated my initial variables to triggerS and triggerT to avoid conflict//
private var triggerSActivated : boolean = false; private var triggerTActivated : boolean = false; private var timerS : float = 0.0; private var timerT : float = 0.0; private var triggerSObject : GameObject; private var triggerTObject : GameObject; var doorOpenSound : AudioClip; var doorShutSound : AudioClip;
function OnTriggerEnter (hit:Collider){ if((hit.gameObject.tag == "lifttrig_s") && (triggerSActivated == false)){ triggerSObject=hit.gameObject ; print (triggerSObject.name);
doorsS (doorOpenSound,"dooropen", triggerSObject,true);
}
if((hit.gameObject.tag == "lifttrig_t") && (triggerTActivated == false)){
triggerTObject=hit.gameObject ;
print (triggerTObject.name);
doorsT (doorOpenSound,"dooropen", triggerTObject,true);
}
}
function Update() {
if(triggerSActivated)
{
timerS += Time.deltaTime;
}
if(timerS >= 5)
{
doorsS(doorShutSound, "doorshuts", triggerSObject, false);
timerS = 0.0;
}
if(triggerTActivated)
{
timerT += Time.deltaTime;
}
if(timerT >= 5)
{
doorsT(doorShutSound, "doorshuts", triggerTObject, false);
timerT = 0.0;
}
}
function doorsT (aClip: AudioClip ,animName : String, aniTObject :GameObject, triggerTCheck: boolean) { audio.PlayOneShot(aClip); triggerTActivated = triggerTCheck;
aniTObject.transform.parent.animation.Play(animName); }
function doorsS (aClip: AudioClip ,animName : String, aniSObject :GameObject, triggerSCheck: boolean) { audio.PlayOneShot(aClip); triggerSActivated = triggerSCheck;
aniSObject.transform.parent.animation.Play(animName);
} @script RequireComponent(AudioSource)
It is solid and transparent but you can treat them as Left and Right.
Answer by alexnode · Apr 05, 2010 at 04:39 PM
Dump!! me, sorry, I just had to store the animation on the root object, and have only one trigger that plays all the door animations. I separated the model with the different animation (Lift in my case) to another model so that the root gameObject doesn't play the lift animation too. the code looks much better now.
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 == "doorani") && (doorIsOpen == false)){ doorObject=hit.gameObject ; doors (doorOpenSound,"dooropen", doorObject,true);
}
}
function Update() {
if(doorIsOpen)
{
timer += Time.deltaTime;
}
if(timer >= 6)
{
doors(doorShutSound, "doorshuts", doorObject, false);
timer = 0.0;
}
}
function doors (aClip: AudioClip ,animName : String, doorObject :GameObject, doorCheck: boolean) {
audio.PlayOneShot(aClip); doorObject.transform.parent.animation.Play(animName); doorIsOpen = doorCheck;
} @script RequireComponent(AudioSource)