Graduation project help!! Sliding door

I’d like to move a sliding door from point A to point B smoothly with a transition.
In my code, I simply changed the coordinates, but that makes it teleport.

How would I make it move(smoothly) and not teleport?

using UnityEngine;
using System.Collections;

public class SlidingDoorScript : MonoBehaviour {

    public Vector3 desiredPosition;  //the position/coordinates to move the door to
    public Vector3 defaultPosition;  //the default position/coordinates of the door

    private bool enter;  //has the player entered the triggering volume
    private Vector3 currentPos;  //current door position
    private bool open;  //is the door open or not

   
    void OnTriggerEnter(){
        enter = true;
    }

    void OnTriggerExit(){
        enter = false;
    }
   
    void Update()
    {

        if (enter)
        {
            currentPos = transform.position;  // get current door position
            print (currentPos);  //for debuging

            if (open){  //if open is true...
                transform.position = desiredPosition;  //...moves door to desired position
            }

            if (!open){  //if open is false...
                transform.position = defaultPosition;  //...moves door to default position
            }

            if (Input.GetKeyDown("e")){  //on e-pressed toggle var open
                open = !open;
            }

        }
    }
}

It’s for my graduation project

Alright. You can do this by using Vector3.Lerp
http://docs.unity3d.com/ScriptReference/Vector3.Lerp.htmlGood Lucl

Ok so I used: transform.position = Vector3.Lerp(desiredPosition, defaultPosition, Time.deltaTime * speed); and it still teleports.
I tried using Time.time, but still nothing.

using UnityEngine;
using System.Collections;

public class SlidingDoorScript : MonoBehaviour {

    public Vector3 desiredPosition;  //the position/coordinates to move the door to
    public Vector3 defaultPosition;  //the default position/coordinates of the door
    public float speed = 1.0f;

    private bool open;  //is the door open or not
    private bool enter;  //has the player entered the triggering volume

    void OnTriggerEnter(){
        enter = true;
    }

    void OnTriggerExit(){
        enter = false;
    }
 
    void Update()
    {
        if (enter)
        {
            if (open){  //if open is true...
                transform.position = Vector3.Lerp(desiredPosition, defaultPosition, Time.deltaTime * speed);  //...moves door to desired position
            }

            if (!open){  //if open is false...                 
                transform.position = Vector3.Lerp(defaultPosition, desiredPosition, Time.deltaTime * speed);  //...moves door to default position
            }

            if (Input.GetKeyDown("e")){  //on e-pressed toggle var open
                open = !open;
            }
        }
    }
}

Based on Time.deltaTime * 1, this should be moving 0.1 seconds forward

even Vector3.SmoothDamp won’t work

did you assign default position and desired position, coz from the script theyre not assigned

yep.
Vector3 defaultPosition = -5.901, -0.003, 3.534
Vector3 desiredPosition = -5.466, -0.003, 3.959

Alright, U made me try by m own. :slight_smile: Since its graduation, since Im a student myself, im definitly gonna try to help you out :slight_smile:

thanx m8 :slight_smile:

Ok, I got it working.
So without further talking here is the script of the player

publicTransformdoor;
boolenter = true;
boolopen = false;
publicVector3desiredPos;
publicVector3defaultPos;
Rigidbodyrigid;

voidStart(){
      defaultPos = door.transform.position;
      rigid = door.GetComponent<Rigidbody>();
}
voidUpdate(){
      Move();
      if(enter){
             if(open){
                     //Check if the door went in the position we wanted it
                    if(door.transform.position.x <= desiredPos.x)
                    { 
                          open = false;
                    }
                    else
                    {
                          //If the deired position is not reached, move the door in that position smoothly. Chage speed by adding another variable and multiplaying Time.deltaTime.
                          rigid.MovePosition(door.transform.position - door.transform.right * Time.deltaTime);
                    }
             }
       }
}
voidMove(){
         RigidbodyrigidBody = GetComponent<Rigidbody>();
         rigidBody.MovePosition(transform.position - transform.forward * Time.deltaTime);
}
void OnTriggerEnter(Collider other){
         if(other.gameObject.tag == "Trigger"){
                Debug.Log ("Entered");
                open = true;
         }
}
void OnTriggerExit(Collider other){
        if(other.gameObject.tag == "Trigger"){
                Debug.Log ("Left");
                open = false;
}
}

Edit: rigid.MovePosition(door.transform.position - door.transform.right * Time.deltaTime);
this code makes smooth transition. We run these code unitl we go to the desired position.
-door.transform.right is the same as +door.transform.left, so the door goes in the left :slight_smile:

Or you could use an animation…

It’s not a real graduation. Since they didn’t teach much in the course…

If I had to do this via code I would use a coroutine. However an animation is a probably a better choice.

The way you are using lerp is incorrect. If you want to do it like that then you need Vector3.MoveTowards:

Lerp returns a point a percentage of the way between both point depending on that final number.

So in a situation where pointA = (0, 5, 0) & pointB = (0, 10, 0) then lerp would work as follows:

Vector3.Lerp(PointA, PointB, 0.5f); // = (0, 7.5, 0) - The halfway point (0.5 being 50%)
Vector3.Lerp(PointA, pointB, 0.0f); // = (0, 5, 0) - The start & 1.0f being the end so (0, 10, 0)

Time.deltaTime gives you the time between frames, this would be a tiny number about 0.03f or something stupidly low, so you are barely moving along the path.

You need to store an incremental value like below:

float prog = 0.0f;

prog += Time.deltaTime;

if(prog > 1.0f)
{
prog = 1.0f;
}

transform.position = Vector3.Lerp(pointA, pointB, prog);

Hope this helps.

1 Like

Hey everyone. Thanks for the help! I’ve couldn’t have done it without you.

EETechnology: thanks for taking your time man :slight_smile: it helped

BoredMormon: PS I’m attending mechatronics, in which they don’t teach this sorts of stuff, rather robots, machines, CNC,CAD… I’m making an exclusive final year project for my graduation. Everyone in my program is making these tiny robots, 3D printers… I’m making a project in Unity and if you open a door in Unity, the door opens in real life; if you lit a light, the light lits in real life…

lordconstant: hey thanks for the information and the code! I used it in mine and it works :slight_smile:

Here is the code:

using UnityEngine;
using System.Collections;

public class SlidingDoorScript : MonoBehaviour {

    public Vector3 desiredPosition;  //the position/coordinates to move the door to
    public Vector3 defaultPosition;  //the default position/coordinates of the door
    public float speed = 0.3f;  //the speed of the opening/closing

    private bool firstOpen = false;  //var determening if the door has been opened at least once
    private bool open;  //is the door open or not?
    private bool enter;  //has the player entered the triggering volumes?
    private float prog;  //for calculating path, the door has traveled


    void OnGUI()  //GUI text
    {
        GUIStyle myStyle = new GUIStyle(GUI.skin.GetStyle("label"));
        myStyle.fontSize = 25;
       
        if (enter == true) {
            if (open == false) {
                GUI.Label (new Rect (Screen.width / 2 - 75, Screen.height - 300, 150, 50), "E - open",myStyle);
            } else {
                GUI.Label (new Rect (Screen.width / 2 - 75, Screen.height - 300, 150, 50), "E - close",myStyle);
            }
        }
    }   

    void OnTriggerEnter(){
        enter = true;
    }

    void OnTriggerExit(){
        enter = false;
    }
   
    void Update()
    {
        if (enter) {
            if (open) {
                prog = Path (speed);
                transform.position = Vector3.Lerp(defaultPosition,desiredPosition, prog);  //moves door to desiredPos
            }

            if (!open){
                prog = Path (speed);
                if (firstOpen){  //if the door has been opened at least once (this prevents the door from moving to desired position on start)
                    transform.position = Vector3.Lerp(desiredPosition,defaultPosition, prog);  //moves door to desiredPos
                }

                if (transform.position != desiredPosition){  //if on start the door is not on desired position...
                    transform.position = Vector3.Lerp (defaultPosition, transform.position, 1);  //...move it to desired position
                }
            }

            if (Input.GetKeyDown("e")){  //on e-pressed toggle bool open
                open = !open;
                firstOpen = true;  //door has been opened once
                prog = 0.00f;  //reset path for next opening/closing
            }
        }else if (!enter)  //this if is to ensure that, if the player is outside the triggering, then finish closing/opening the door without stoping
        {
            if (open) {
                prog = Path (speed);
                transform.position = Vector3.Lerp(defaultPosition,desiredPosition, prog);  //moves door to desiredPos
            }
           
            if (!open){
                prog = Path (speed);
                if (firstOpen){  //if the door has been opened at least once (this prevents the door from moving to desired position on start)
                    transform.position = Vector3.Lerp(desiredPosition,defaultPosition, prog);  //moves door to desiredPos
                }
            }

            if (transform.position != desiredPosition){  //if on start the door is not on desired position...
                transform.position = Vector3.Lerp (defaultPosition, transform.position, 1);  //...move it to desired position
            }
        }
    }

    //smoothly calculating path
    float Path (float speed)
    {
        prog += Time.deltaTime * speed;
        if (prog >= 1) {
            prog = 1.0f;
        }
        return prog;
    }
}

This works even if the door is rotated and has normal-space coordinates, not global.

Wish I’d seen this topic right away, as this is the exact example used in the Mecanim video tutorial in Unity Lessons. Yeah, it’s most of an hour long, but completely worth it to show you how amazing Mecanim is.

1 Like

Oh, that actually sounds cool. I almost switched to machatronics half way through my degree. Please excuse my snarky comment before. We get a lot of kids at game dev school asking us to do their homework for them.

Looking at your code it does seem excessively long for simple opening and closing of a door.

I would do something like this:

// Set this however you like
Vector3 openPosition;
Vector3 closedPosition;
float maxSpeed;

bool isDoorOpening;

void Update () {
    Vector3 target;
    if (isDoorOpening) {
        target = openPosition;
    } else {
        target = closedPosition;
    }
    transform.Position = Vector3.MoveTowards(transform.position, target, maxSpeed * Time.deltaTime);
}