Skip to content

Instantly share code, notes, and snippets.

@ysalihtuncel
Last active April 5, 2025 18:52
Show Gist options
  • Save ysalihtuncel/904c842b442b8eeee657ee41424520ac to your computer and use it in GitHub Desktop.
Save ysalihtuncel/904c842b442b8eeee657ee41424520ac to your computer and use it in GitHub Desktop.
Door
using System.Collections;
using UnityEngine;
public class Door : MonoBehaviour, ISwitchable
{
private bool isActivated;
public bool IsActivated => isActivated;
public Switch switcher;
//Local angles for the door to open and close
public float openAngle = 90f, closeAngle = 0f;
public float duration = 1f;
void Start()
{
switcher.SetClient(this);
}
public void Activate()
{
Open();
isActivated = true;
}
public void Deactivate()
{
Close();
isActivated = false;
}
public void Open()
{
Debug.Log("Door opened!");
StopAllCoroutines();
StartCoroutine(DoorAction(openAngle));
}
public void Close()
{
Debug.Log("Door closed!");
StopAllCoroutines();
StartCoroutine(DoorAction(closeAngle));
}
IEnumerator DoorAction(float angle)
{
float t = 0;
Vector3 startRot = transform.eulerAngles;
Vector3 endRot = new Vector3(0, angle, 0);
while (t < 1)
{
t += Time.deltaTime / duration;
transform.eulerAngles = Vector3.Lerp(startRot, endRot, t);
yield return null;
}
transform.eulerAngles = endRot;
}
}
using UnityEngine;
public interface ISwitchable
{
public bool IsActivated { get; }
public void Activate();
public void Deactivate();
}
using UnityEngine;
public class Switch : MonoBehaviour
{
ISwitchable client;
public void SetClient(ISwitchable client)
{
this.client = client;
}
public void Toggle()
{
if (client.IsActivated)
{
client.Deactivate();
}
else
{
client.Activate();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment