52 lines
1.2 KiB
C#
52 lines
1.2 KiB
C#
using UnityEngine;
|
|
|
|
public class BukaPintu1 : MonoBehaviour
|
|
{
|
|
public Transform door;
|
|
public float openAngle = 90f;
|
|
public float closeAngle = 0f;
|
|
public float smoothSpeed = 2f;
|
|
private bool open = false;
|
|
|
|
public AudioSource audioSource;
|
|
public AudioClip bukaPintuSound; // Suara pintu buka
|
|
private bool soundPlayed = false; // Agar suara tidak berulang
|
|
|
|
|
|
void Update()
|
|
{
|
|
|
|
Quaternion targetRotation = open ?
|
|
Quaternion.Euler(0, openAngle, 0) :
|
|
Quaternion.Euler(0, closeAngle, 0);
|
|
|
|
// Rotasi pintu
|
|
door.localRotation = Quaternion.Slerp(door.localRotation, targetRotation, Time.deltaTime * smoothSpeed);
|
|
}
|
|
|
|
private void OnTriggerEnter(Collider other)
|
|
{
|
|
if (other.CompareTag("Player"))
|
|
{
|
|
open = true;
|
|
}
|
|
|
|
if (!soundPlayed && audioSource != null && bukaPintuSound != null)
|
|
{
|
|
audioSource.PlayOneShot(bukaPintuSound);
|
|
soundPlayed = true;
|
|
}
|
|
|
|
}
|
|
|
|
private void OnTriggerExit(Collider other)
|
|
{
|
|
if (other.CompareTag("Player"))
|
|
{
|
|
open = false;
|
|
}
|
|
|
|
soundPlayed = false;
|
|
}
|
|
}
|