[Solved] Sliding garage door using transform.translate.

I’m trying to make a door that works like this:

  • when a player enters trigger zone he becomes capable of initiating the opening/closing of said door with a button
  • the door stops moving when it reaches a point 2 units higher in the y axis than it’s original position (if it’s opening)
  • the door stops moving when it reaches it’s original position if it’s closing
  • the player can switch directions of the door moving mid-move

So i made those 2 scripts and both don’t work (i’m excluding some parts for clarity eg. the trigger events that set player_inrange variable,):

private bool player_inrange = false;
    private Transform state_closed;
    private Transform state_open; //transform that door has to have after it's opened
    private bool wanted_open = false;
    public float speed;

    void Start()
    {
        state_closed = gameObject.transform;
        state_open.position = state_closed.position + new Vector3(0f, 1f, 0f);
    }

 void Update()
    {
        if (Input.GetButtonDown("Use") && player_inrange == true) //if player is in range and presses e(default)
        {
            wanted_open = !wanted_open;
            Debug.Log("You want to switch the door"); //i get this one
        }
        if (wanted_open == true) //if they want to be open
        {
            transform.position = Vector3.MoveTowards(transform.position, state_open.position, Time.deltaTime * speed);
            Debug.Log("the door should be opening"); //but not this one
        }
        if (wanted_open == false) //if they want to be closed
        {
            transform.position = Vector3.MoveTowards(transform.position, state_closed.position, Time.deltaTime * speed);
        }
    }

I don’t really know why this isn’t working. I get the first debug log, but not the second.

Now, this is the different script i’ve tried. This one uses regular transform.translate, but should stop the moment it reaches the thing specified in if statements, but… it doesn’t, instead it stops in random places - the higher I make the speed variable the faster it goes, but stops further and further away than it should.

void Start()
    {
        current_transform = gameObject.transform;
        state_closed = gameObject.transform;
        state_open.position = state_closed.position + new Vector3(0f, 1f, 0f);
}

void Update()
    {
        current_transform = gameObject.transform;
        if (Input.GetButtonDown("Use") && player_inrange == true) //if player is in range and presses e(default)
        {
            wanted_open = !wanted_open;
            Debug.Log("You want to switch the door");
        }
        if (wanted_open == true && current_transform != state_open) //if they want to be open, but aren't (it should stop when it reaches state_open)
        {
            gameObject.transform.Translate(Vector3.up * Time.deltaTime * speed);
            Debug.Log("the door should be opening");
        }
        if (wanted_open == false && current_transform != state_closed) //if they want to be closed, but aren't (it should stop when it reaches state_closed)
        {
            gameObject.transform.Translate(Vector3.down * Time.deltaTime * speed);
        }
    }

So it’s weird and doesn’t work and at this point every other question that asks for sliding doors or “how to move an object over time” seems like gibberish to me, becasue i can’t find what’s wrong.

So I changed the script in some ways, the most notable thing is that the if statements check for a float that is the value of the y axis, because I don’t really need the other axes.

using UnityEngine;
using System.Collections;

public class Door : MonoBehaviour {

    //use this script for doors that require pressing the use button
    //set up the use button in edit >> project settings >> input
    //the door has to have a trigger

    private bool player_inrange = false; //is player in range of the door
    private float state_closed; //the value of y axis position when the door is closed
    private float state_open; //the value of y axis position when the door is open
    private bool wanted_open = false; //if the door wants to be open or closed
    private float current_y; //y axis position of the door, updated every frame
    public float speed; //how fast the door moves
    

    void Start()
    {
        state_closed = gameObject.transform.position.y;
        state_open = state_closed + 3f;
    }

    void OnTriggerEnter(Collider other)
    {
        if (other.name == "Player")
        {
            player_inrange = true;
            CancelInvoke("IdleCloseDoor");
            Debug.Log("You entered the trigger");
        }
    }

    void OnTriggerExit(Collider other)
    {
        if (other.name == "Player")
        {
            player_inrange = false;
            Debug.Log("You left the trigger");
            Invoke("IdleCloseDoor", 5f);
        }
    }



    void Update()
    {
        current_y = gameObject.transform.position.y;
        if (Input.GetButtonDown("Use") && player_inrange == true) //if player is in range and presses e(default)
        {
            wanted_open = !wanted_open;
            CancelInvoke("IdleCloseDoor");
            Debug.Log("You want to switch the door");
        }

        //if they want to be open, but aren't (it should stop when it reaches state_open)
        if (wanted_open == true && current_y < state_open)
        {
            gameObject.transform.Translate(Vector3.up * Time.deltaTime * speed);
        }

        //if they want to be closed, but aren't (it should stop when it reaches state_closed)
        if (wanted_open == false && current_y > state_closed) 
        {
            gameObject.transform.Translate(Vector3.down * Time.deltaTime * speed);
        }
    }


    //and this makes the door want to close when the player wanders away and the door stays open for too long
        void IdleCloseDoor()
    {
        wanted_open = false;
    }
}

Now it works as intended. I’ll change the title of this question, so more people searching for something like this will find it.