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.