I want to make a script where a button is pressed on a wall, and a door slides up into the ceiling, but I have no clue where to start.
I just finished my script for that.
I’ll give you the core of it:
private bool ObjectIsOpen;
private bool BlockInput;
private float Timer;
private Vector3 CurrentPosition;
public float Duration; //Choose slide duration
public Vector3 ClosedPosition; //Add in the inspector the transform position into this variable when the door should be closed
public Vector3 OpenedPosition; //Add in the inspector the transform position into this variable when the door should be open
IEnumerator Slide()
{
BlockInput = true;
Timer = 0.0f;
while (Timer < Duration)
{
if (!ObjectIsOpen)
{
CurrentPosition = Vector3.Lerp(ClosedPosition, OpenedPosition, Timer / Duration);
}
else if (ObjectIsOpen)
{
CurrentPosition = Vector3.Lerp(OpenedPosition, ClosedPosition, Timer / Duration);
}
transform.localPosition = CurrentPosition;
Timer += Time.deltaTime;
yield return null;
}
ObjectIsOpen = !ObjectIsOpen;
if (!ObjectIsOpen)
{
transform.localPosition = ClosedPosition; //Makes sure the door is closed completely at the end
}
else if (ObjectIsOpen)
{
transform.localPosition = OpenedPosition; //Makes sure the door is open completely at the end
}
BlockInput = false;
}
}
You can assign the door coordinates in opened and closed position in the inspector into the vector3 variables and then let the door slide between those two points in choosed duration.