using UnityEngine; /// A desk drawer that slides out when clicked and back in on the next click. [RequireComponent(typeof(Collider))] public class Drawer : MonoBehaviour, ILobbyClickable { [Tooltip("The part that moves (front, handle and box).")] public Transform body; [Tooltip("World direction and distance the drawer pulls out.")] public Vector3 openOffset = new Vector3(0f, 0f, -5f); public float slideTime = 0.35f; Vector3 closedPos; bool open; float t = 1f; public string Hint => open ? "Drawer: click to close" : "Drawer: click to open"; void Start() { if (body != null) closedPos = body.position; } public void Click() { open = !open; t = 0f; } void Update() { if (body == null || t >= 1f) return; t = Mathf.Min(1f, t + Time.deltaTime / Mathf.Max(0.01f, slideTime)); float e = t * t * (3f - 2f * t); Vector3 from = open ? closedPos : closedPos + openOffset, to = open ? closedPos + openOffset : closedPos; body.position = Vector3.Lerp(from, to, e); } }