Timer on a door

Hi,

I’m still new to scripting, and am still learning.

But sometimes it strikes me hard.

So I want to trigger a door when the player hits the button, after that the door will open.

function OnTriggerEnter (other : Collider) {	
	var door = other.gameObject.Find("myDoor");
	door.transform.position.y = 1;
}

This scripting is just fine.

So the next thing I want is that if the door is opened, it will close after a few seconds.

Here’s my idea.

Note: Instead of making it return to it’s original position, I just scripted Destroy because it’s easy to test with.

if(gameObject.Find("myDoor").transform.position.y >= 1) {
	yield WaitForSeconds (3);
	Destroy(gameObject.Find("myDoor"));
}

So this does nothing when I trigger the door. But if i set my ‘if’ comparison to >= 0, the door will be destroyed after 3 seconds. Because my door is at position y = 0.

So why doesn’t this work with >= 1?

Thanks in advance!

Lets take a look at your vars…
Looks lke u could use a bool.
Or just use the condition

   var door : Transform;
   
   function OnTriggerEnter (other : Collider) {    
   if(other.gameObject.tag == "Player"){ 
   door.transform.position.y = 1; 
   Close();
  }
}   
    function Close() {
    if(door.transform.position.y = 1){
    //then close it before the cat gets out
    yield WaitForSeconds (3); 
    door.transform.position.y = 0;
  }
}

UNTESTED AND UNSURE_but may give you some ideas if it doesnt work.

So what kind of door u making :smile:

Well, just a simple door to open and close after a while.

So when the player hits the button (like in the Zelda series), a door will open, and after a few seconds, when the timer runs out, the door will close again.

But I’m just trying to understand some basics.

So I’ve put the first script to the button.

And the second script to the door.

Here I’ve simplified my scripting.

This one’s attached to the button.

function OnTriggerEnter (other : Collider) {
	other.gameObject.Find("myDoor").transform.position.y = 1;
}

And this one’s attached to the door.

if(gameObject.transform.position.y != 0) {
	yield WaitForSeconds (1);
	gameObject.transform.position.y = 0;
}

So, when I hit the button, the door opens, but remains open. It won’t close, I just don’t understand what I’m doing wrong =(

Because if I do this…

if(gameObject.transform.position.y == 0) {
	yield WaitForSeconds (1);
	gameObject.transform.position.y = 1;
}

the door will go to position y=1 as soon as I start the game.

So why does it not work when I set the condition to != 0 ?

So I did this:

var door : Transform;

function OnTriggerEnter () {
	door.transform.position.y = 1;
	Close();
}

function Close() {
	if(door.transform.position.y != 0){
	yield WaitForSeconds (2);
	door.transform.position.y = 0;
	}
}

And attached it to the button. And it worked!
The door opens when the player hits the button, and after 2 seconds the door closes.

Thanks! As this is what I wanted indeed.

But still, I wanted to understand where I went wrong with my idea.

And another thing, you put Close(); in the OnTriggerEnter() function, but I don’t understand why. And to be honest, I don’t even know what it is?

To declare your own custom functions

Close();

I think you can put them anywhere :smile:

I’m still learning too :? Animating is my main thing, i’m Loving JScript/UnityScript though :smile:

Hmm I see, I think I understand…

but can you give me another example of how and when to declare your own custom functions?

Because if I put it somewhere else, it doesn’t work anymore.

And I still would appreciate if someone could tell me what was wrong with my original scripting.

It explains this a little in the Coroutine section of the script reference manual. and also here.

I’m not sure but i thing the reason your original code would’nt work is cause it was in the OnTriggerEnter that is only called once you enter, and needed to be inside a function that will be checked more often. OnTriggerExit or OnTriggerStay may have worked.

Also Custom functions are described here.

http://forum.unity3d.com/viewtopic.php?t=36096

Hope this helps Im new to coroutines and custom functions as well :smile:

Hi, thanks for the script. I used it at different object and it always worked.

i found your firs idea a well interesting. To make the script search for specific object and then call a fuction on it. But i heard that would be very resorce consuming ?

Anyway is there a way to lett the door move gently, at the moment it just kicks into the new position in one frame. I think there are premade easing funktion sin unity.

If you’re interested in watching it, the “door that automatically closes” is one of many things covered in the Unity tutorials here:

http://learnmesilly.com/

Lesson 2, Part 5 I think…

Hey, thanks all for trying to help, but I’ve solved my own problem.

Here’s what I did.

First of all I wrote the script that’s attached to the button.

function OnTriggerEnter(other : Collider)
{
	other.gameObject.Find("pCube3").transform.position.y = 1;
}

This one open’s the door when the button is pressed or touched whatever.

Second of all, here’s my improved script that works when attached to the door.

function Close(){
if(gameObject.transform.position.y != 0) {
	yield WaitForSeconds (2);
	gameObject.transform.position.y = 0;
}
}

function Update(){
	Close();
}

You see, my mistake was, I wrote the if statement blank in the code, so it would be initiated as soon as the game would start.

Now I just create my Close() function and write what I want, to close the door after 2 seconds when it’s open.

And than in the Update() function, I just call the Close() function every frame to check if the door’s open or not. =)

So this was just for basic understanding.

But Blackmantis, I think your scripting is better, because it just calls the Close() function when the trigger is entered, so it doesn’t calls the Close() function every frame. Anyway, thanks for your help, you’re a really nice guy! =)

Yes I know, this one just jumps to another position. But it was just for basic and fundamental understanding of the scripting method. So you can make animations if you want, that opens a door smoothly, and than just call the animation when the trigger is on.

This method can be found at http://unity3d.com/support/documentation/Manual/Character-Animation.html

Yes, thank you, I will check that out. But I posted this topic just to understand what was wrong in my idea. But I have solved that now. So my question is no more. =)

Thanks, and sorry for all the trouble if I made any.