I got a door, wich is the child of an empty object with “is trigger” activated.
what I cant find is: how to make the door open when i (reach the empty object + i press a key) without using an animation ??
i guess its using “transform.Translate” but the problem is, i want to make a time to wait before the door close back, and a level to reach ( not too high) before it close back.
so far i got:
var speed : int = 200;
function OnTriggerStay () {
if (Input.GetKeyUp("e") ) {
//animation.Play ();
transform.Translate(0, 0, speed * Time.deltaTime);
}
}
Here’s some code I have for sliding a door up with a trigger object.
// simple lerp up and down movement
var targeti : Transform;
var moveSpeed : float = 5.0;
var upMaxDistance : float = 10.0;
var openCloseSound : AudioClip;
private var initPosition : Vector3;
private var openDoor : boolean = false;
function Start() {
initPosition = targeti.transform.position;
}
function Update () {
if (!targeti) {
return;
}
if (openDoor == true) {
targeti.position.y = Mathf.Min(upMaxDistance, targeti.position.y+moveSpeed * Time.deltaTime);
}
else {
targeti.position.y = Mathf.Max(initPosition.y, targeti.position.y-moveSpeed * Time.deltaTime);
}
}
function OnTriggerEnter() {
openDoor = true;
audio.PlayOneShot(openCloseSound);
}
function OnTriggerExit() {
yield WaitForSeconds(2);
openDoor = false;
audio.PlayOneShot(openCloseSound);
}
Pretty easy to use, 1st make a trigger object in front of the door, this would be the object you hit to open the door, 2nd on your trigger object, drag the door to the targeti slot. You also have audio clips you can drag in to play an audio sound.
BTW could you explain me please why is the “Mathf” ?
isnt it a simplier way to move the door ?
is there a way to just say " translate from here to here" ?
anyway it works very well, its just to understand.
Thank you for this great script but I was wondering how to change its axis to make the door moves left/right instead of up. Because atm it only goes up and i have tried to change y axis to x from script but didn’t work… Any ideas guys? Thanks
the last post is old and I don’t mean to bring the thread to life just like that… but, I wanted to thank for the code and answer eko with this modifications from the code to make elevator doors
// simple lerp up and down movement
var targeti : Transform;
var targetu : Transform;
var moveSpeed : float = 0.7;
var upMaxDistance : float = 0;
var openCloseSound : AudioClip;
private var initPositioni : Vector3;
private var initPositionu : Vector3;
private var openDoor : boolean = false;
function Start() {
initPositioni = targeti.transform.position;
initPositionu = targetu.transform.position;
}
function Update () {
if (!targeti) {
return;
}
if (!targetu) {
return;
}
if (openDoor == true) {
targeti.position.z = Mathf.Min(upMaxDistance, targeti.position.z+moveSpeed * Time.deltaTime);
targetu.position.z = Mathf.Max(upMaxDistance, targetu.position.z-moveSpeed * Time.deltaTime);
}
else {
targeti.position.z = Mathf.Max(initPositioni.z, targeti.position.z-moveSpeed * Time.deltaTime);
targetu.position.z = Mathf.Min(initPositionu.z, targetu.position.z+moveSpeed * Time.deltaTime);
}
}
function OnTriggerEnter() {
yield WaitForSeconds(1);
openDoor = true;
audio.PlayOneShot(openCloseSound);
}
function OnTriggerExit() {
yield WaitForSeconds(1);
openDoor = false;
audio.PlayOneShot(openCloseSound);
}
Hey Ga2z. I tried this but im missing something, somehow… First id like to be able to open the door pressing the E key, and also want to know how to make the door opens/closes if i press a button or level in the map… like for example your unlocking a security door with a computer or something like that… anyways Ill pareciate a lot if you help me with this. Thanks in advance
Hi there, i tried your code, but x-axis was moved on z-axis away from door frame, then when switched to z-axis moved in front of door frame? i used default cubes