47 lines
1.1 KiB
C#
47 lines
1.1 KiB
C#
using UnityEngine;
|
|
|
|
public class BukaLemari : MonoBehaviour
|
|
{
|
|
public Transform leftDoor;
|
|
public Transform rightDoor;
|
|
public float openAngle = 90f;
|
|
public Transform player;
|
|
public float interactDistance = 2f;
|
|
|
|
private bool isOpen = false;
|
|
private bool hasInteracted = false;
|
|
|
|
void Update()
|
|
{
|
|
float distance = Vector3.Distance(player.position, transform.position);
|
|
|
|
if (distance <= interactDistance && !hasInteracted)
|
|
{
|
|
// Player mendekat, buka pintu
|
|
OpenDoors();
|
|
hasInteracted = true;
|
|
}
|
|
|
|
if (distance > interactDistance && isOpen)
|
|
{
|
|
// Player menjauh, tutup pintu
|
|
CloseDoors();
|
|
hasInteracted = false;
|
|
}
|
|
}
|
|
|
|
void OpenDoors()
|
|
{
|
|
isOpen = true;
|
|
leftDoor.localRotation = Quaternion.Euler(0, openAngle, 0);
|
|
rightDoor.localRotation = Quaternion.Euler(0, -openAngle, 0);
|
|
}
|
|
|
|
void CloseDoors()
|
|
{
|
|
isOpen = false;
|
|
leftDoor.localRotation = Quaternion.Euler(0, 0, 0);
|
|
rightDoor.localRotation = Quaternion.Euler(0, 0, 0);
|
|
}
|
|
}
|