Files
GameDevTVTutFPS/Assets/Scripts/Door.cs

105 lines
2.2 KiB
C#
Raw Normal View History

2026-01-13 15:43:33 +00:00
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.Rendering;
public class Door : MonoBehaviour, IInteractable
{
[SerializeField] bool autoDoor;
[SerializeField] bool locked;
[SerializeField] bool isOpen;
[SerializeField] GameObject door;
[SerializeField] float openHeight = 3f;
[SerializeField] float openTime = 2f;
void Awake()
{
}
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
}
// Update is called once per frame
void Update()
{
}
void OnTriggerEnter(Collider other)
{
if(other.CompareTag("Player") && autoDoor && !locked && !isOpen)
{
OpenDoor(openHeight);
isOpen = true;
}
else if(other.CompareTag("Player") && autoDoor && locked)
{
Debug.Log("The door is locked.");
}
}
void OnTriggerExit(Collider other)
{
if(other.CompareTag("Player") && autoDoor && isOpen)
{
CloseDoor(openHeight);
isOpen = false;
}
}
void OpenDoor(float height)
{
door.transform.Translate(0f,-height,0f);
}
void CloseDoor(float height)
{
door.transform.Translate(0f,height,0f);
}
public void Interact(GameObject interactor)
{
2026-01-13 17:33:30 +00:00
if (!autoDoor)
2026-01-13 15:43:33 +00:00
{
2026-01-13 17:33:30 +00:00
if (!locked && !isOpen)
{
OpenDoor(openHeight);
isOpen = true;
}
else if (!locked && isOpen)
{
CloseDoor(openHeight);
isOpen = false;
}
else if (locked)
{
Debug.Log("The door is locked.");
}
2026-01-13 15:43:33 +00:00
}
else
{
2026-01-13 17:33:30 +00:00
return;
2026-01-13 15:43:33 +00:00
}
}
public bool CanInteract(GameObject interactor)
{
2026-01-13 17:33:30 +00:00
return !locked;
2026-01-13 15:43:33 +00:00
}
public string GetInteractionPrompt()
{
2026-01-13 17:33:30 +00:00
if (locked)
return "Door is locked";
return isOpen ? "Press [E] to close door" : "Press [E] to open door";
2026-01-13 15:43:33 +00:00
}
public void OnHighlight()
{
// e.g. outline effect
}
public void OnUnhighlight()
{
// remove outline
}
}