Empty GO as Trigger

Hello All,

I’ve been trying to use empty GO as trigger to gradually open a door upwards and was doing okay until I wanted the door to stop moving.

I tried using another trigger collider to stop the door when the door enters the new trigger but not getting any luck. below is the script, can anybody tell me what I’m doing wrong?

//Attached script to empty GO and check isTrigger in inspector

var target : Transform;          //target is entrancedoor1

 //conditinal switch for Update to execute translate
var OnOFF = 0.0;     


function OnTriggerEnter () 
{
	OnOFF = 1;  //turn switch to On

}

function Update()
{
	if(OnOFF == 1)
	{
		
		target.transform.Translate(Vector3.up*Time.deltaTime);
	 	
	 }	 	
	
}

In the inspector, if I manually turn the OnOFF to 0, the door stops moving.

I tried adding

if(target.transform.position.y == 3.0) // 3.0 is door height above floor
{
  OnOFF = 0;
}

But not doing anything.

Thanks,
Ray

2 things to consider:

  1. You might want to use a true/false boolean to keep track of this situation. It’s as easy as:
var OnOff = false;  // initial value

function OnTriggerEnter (col : Collider) // don't forget to reference the Collider that enters the trigger...
{ 
   OnOff = true;  //turn switch to On 

}

// and finally

if (OnOff == true) {  // check to see if the door should start moving
    // move door code
}
  1. Since you’re moving the door at a variable rate (depending on framerate), the odds of the door’s y position landing EXACTLY on 3.0 for a frame is very unlikely. You could replace that line with:
// the == check becomes a >= check, for "greater than or equal to"

if(target.transform.position.y >= 3.0) {
    //stop the door
}

:smile:

Thank you!

Ray

:wink:

You’re welcome! Is it working out for you now?

It’s flawless!