Hello there, im new to coding in java, but it seems easy. Im trying to create an event where if you touch a door, it opens for 10 sec then closes. If you go through, it will transfer you to a different level. I need to code this, but i dont know any of the variables or commands. Could someone help me, code it then explain it or let me look at it and remember it.
You could make a trigger that if entered, door opens, or if you raycast towards the door and are within distance / is hit (or is clicked) to open the door, or use Vector3.Distance to see if you’re in distance to the door.
Once in distance, you can wait for 10seconds similarly by:
var wasNear : boolean = false;
var timer : float = 0;
function Update(){
if (wasNear == true){
timer += 1 * Time.deltaTime;
if (timer > 10.0){
Application.LoadLevel ("someLevel");
}
}
}
and use Application.LoadLevel to load a level:
Thank you very much. Its a good head start and much is appreciated!
You can also use
yield WaitForSeconds(10);
This pauses the execution of the code for 10 seconds, and then continues after that.
ok so im trying this out, but i have a small problem. What script do i use to let unity know if the player touches the door?
Okay, I don’t know if this will work, but you could make 2 different animations:
the first one opens the door (rotates it 90 degrees or whatever)
the second one closes the door (reverse rotates the door the x-amount of degrees that the “open animation” opened the door)
then create a collider on the object that you want to trigger the door, and mark it as trigger.
Then add a script like this:
var opened : boolean = false;
function OnTriggerEnter(other : Collider){
if other.gameObject.tag == "Player" opened == false){
animation.Play(openAni);
} else if(other.gameObject.tag == "Player" opened == true){
animation.Play(closeAni);
}
}
openAni is your animation of the door opening
closeAni is the animation of the door closing
Note that I have no idea if this will work, since I’m new to JS myself. You will have to add the part about closing the door after 10 seconds yourself. I guess you could use the yield WaitFOrSeconds(10); here…