sliding door openning (without animation) problem.

Hi guys,

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);
	  
   } 
    
}

thank you.

1 Like

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.

Hope that helps you out.

-Raiden

thx very much Raiden !

all the elements i needed are in your script !
its great !

thx again.

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.

thx

The Mathf.Min and Mathf.Max calls are to make sure the door position stops at the exact right place at the end of the animation.

Well said andeeee, thank you :slight_smile: Glad you liked the script PARADOKS.

-Raiden

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 :frowning: and i have tried to change y axis to x from script but didn’t work… Any ideas guys? Thanks

Any ideas guys ?? Please help :frowning:

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);
    }

and THANKS!!

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

Hello Everyone,

Here is an updated code that will work with more recent versions on unity and has additional features:

  1. Support for left/right sliding doors
  2. Choice between movement along X or Z axis
  3. Stop code from checking and adjusting position endlessly inside update when not needed
    public enum MoveAxis {
        xAxis,
        zAxis
    };
    public MoveAxis moveAxis;

    public Transform leftDoor;
    public Transform rightDoor;
    public float moveSpeed = 0.7f;
    public float maxDistance = 1.2f;
    public AudioSource Speaker;
    public AudioClip openCloseSound;

    // Simple lerp left & right movement
    private float initialPosLeftDoor;
    private float initialPosRightDoor;
    private float targetPosLeftDoor;
    private float targetPosRightDoor;
    private float newPosLeftDoor;
    private float newPosRightDoor;
    private float currentPosLeftDoor;
    private float currentPosRightDoor;
    private int openDoor = 2;

    void Start() {
        if (moveAxis.ToString() == "xAxis") {
            initialPosLeftDoor = leftDoor.position.x;
            initialPosRightDoor = rightDoor.position.x;
        } else {
            initialPosLeftDoor = leftDoor.position.z;
            initialPosRightDoor = rightDoor.position.z;
        }
    }

    void Update() {
        if (!leftDoor) {
            return;
        }

        //Get current door position
        if (openDoor == 0 || openDoor == 1) {
            if (moveAxis.ToString() == "xAxis") {
                currentPosLeftDoor = leftDoor.position.x;
                currentPosRightDoor = rightDoor.position.x;
            } else {
                currentPosLeftDoor = leftDoor.position.z;
                currentPosRightDoor = rightDoor.position.z;
            }
        }

        //Open door
        if (openDoor == 1) {
            targetPosLeftDoor = initialPosLeftDoor + maxDistance;
            targetPosRightDoor = initialPosRightDoor - maxDistance;

            if (currentPosLeftDoor == targetPosLeftDoor) {
                openDoor = 2; //Do nothing
            } else {
                newPosLeftDoor = Mathf.Min(targetPosLeftDoor, currentPosLeftDoor + moveSpeed * Time.deltaTime);
                leftDoor.position = new Vector3(leftDoor.position.x, leftDoor.position.y, newPosLeftDoor);

                newPosRightDoor = Mathf.Max(targetPosRightDoor, currentPosRightDoor - moveSpeed * Time.deltaTime);
                rightDoor.position = new Vector3(rightDoor.position.x, rightDoor.position.y, newPosRightDoor);
            }

        //Close door
        } else if (openDoor == 0) {
            targetPosLeftDoor = initialPosLeftDoor;
            targetPosRightDoor = initialPosRightDoor;

            if (currentPosLeftDoor == targetPosLeftDoor) {
                openDoor = 2; //Do nothing
            } else {
                newPosLeftDoor = Mathf.Max(targetPosLeftDoor, currentPosLeftDoor - moveSpeed * Time.deltaTime);
                leftDoor.position = new Vector3(leftDoor.position.x, leftDoor.position.y, newPosLeftDoor);

                newPosRightDoor = Mathf.Min(targetPosRightDoor, currentPosRightDoor + moveSpeed * Time.deltaTime);
                rightDoor.position = new Vector3(rightDoor.position.x, rightDoor.position.y, newPosRightDoor);
            }
        }
    }

    void OnTriggerEnter() {
        openDoor = 1;

        //Play Sound
        Speaker.clip = openCloseSound;
        Speaker.Play();
        Speaker.volume = 0.6f;

        Debug.Log("Open Door!");
    }

    void OnTriggerExit() {
        StartCoroutine(DelayClose(2f));
    }

    IEnumerator DelayClose(float time) {
        Debug.Log("Delay Move");
        yield return new WaitForSeconds(time);

        openDoor = 0;

        //Play Sound
        Speaker.clip = openCloseSound;
        Speaker.Play();
        Speaker.volume = 0.6f;

        Debug.Log("Close Door!");
    }

To use:

  1. Create a cube to act as a trigger and place it mid way through the door
  2. Deactivate mesh renderer component to make it transparent
  3. Add a box collider and set it to isTrigger
  4. Drag Script to cube trigger and fill fields in inspector
  5. If you only have one sided door create a dummy side and make it transparent

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