2025-10-22 17:34:58 +08:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
using Script.Gameplay.Connect;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Script.Gameplay.Facility
|
|
|
|
|
|
{
|
|
|
|
|
|
public class PressurePlateController : BaseFacilityController
|
|
|
|
|
|
{
|
|
|
|
|
|
[SerializeField] private LayerMask detectLayer = ~0; // 检测所有层,可在Inspector中指定
|
|
|
|
|
|
[SerializeField] private Vector3 plateSize = new Vector3(1, 0.2f, 1);
|
|
|
|
|
|
[SerializeField] private Vector3 plateOffset = Vector3.up * 0.1f;
|
|
|
|
|
|
|
2025-10-29 12:01:33 +08:00
|
|
|
|
[SerializeField] private GameObject pressTopPrefab;
|
2025-10-29 17:05:10 +08:00
|
|
|
|
|
|
|
|
|
|
[SerializeField] private AudioClip pressedSound;
|
|
|
|
|
|
[SerializeField] private AudioClip returnSound;
|
|
|
|
|
|
[SerializeField] private AudioSource plateSound;
|
2025-10-22 17:34:58 +08:00
|
|
|
|
private bool lastState = false;
|
2025-10-28 08:50:06 +08:00
|
|
|
|
private bool hasObject = false;
|
2025-10-22 17:34:58 +08:00
|
|
|
|
|
|
|
|
|
|
private void FixedUpdate()
|
|
|
|
|
|
{
|
2025-10-23 09:26:47 +08:00
|
|
|
|
if (!isOpenInEditor) return;
|
2025-10-29 12:48:44 +08:00
|
|
|
|
hasObject = Physics.CheckBox(transform.position + plateOffset, plateSize * 0.5f, Quaternion.identity,
|
|
|
|
|
|
detectLayer);
|
2025-10-22 17:34:58 +08:00
|
|
|
|
if (hasObject != lastState)
|
|
|
|
|
|
{
|
|
|
|
|
|
SendSignal(hasObject, this.gameObject);
|
|
|
|
|
|
lastState = hasObject;
|
2025-10-28 08:50:06 +08:00
|
|
|
|
if (hasObject)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 被压下动画
|
2025-10-29 12:48:44 +08:00
|
|
|
|
if (pressTopPrefab != null)
|
2025-10-29 17:05:10 +08:00
|
|
|
|
{
|
2025-10-29 12:48:44 +08:00
|
|
|
|
pressTopPrefab.transform.localPosition = new Vector3(0, 0, 0.1f);
|
2025-10-29 17:05:10 +08:00
|
|
|
|
plateSound.clip = pressedSound;
|
|
|
|
|
|
plateSound.Play();
|
|
|
|
|
|
}
|
2025-10-28 08:50:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
// 弹起动画
|
2025-10-29 12:48:44 +08:00
|
|
|
|
if (pressTopPrefab != null)
|
2025-10-29 17:05:10 +08:00
|
|
|
|
{
|
2025-10-29 12:48:44 +08:00
|
|
|
|
pressTopPrefab.transform.localPosition = new Vector3(0, 0, 1);
|
2025-10-29 17:05:10 +08:00
|
|
|
|
plateSound.clip = returnSound;
|
|
|
|
|
|
plateSound.Play();
|
|
|
|
|
|
}
|
2025-10-28 08:50:06 +08:00
|
|
|
|
}
|
2025-10-22 17:34:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-10-29 12:48:44 +08:00
|
|
|
|
|
2025-10-22 17:34:58 +08:00
|
|
|
|
#if UNITY_EDITOR
|
|
|
|
|
|
private void OnDrawGizmosSelected()
|
|
|
|
|
|
{
|
|
|
|
|
|
Gizmos.color = Color.yellow;
|
|
|
|
|
|
Gizmos.DrawWireCube(transform.position + plateOffset, plateSize);
|
|
|
|
|
|
}
|
|
|
|
|
|
#endif
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|