feat(Transport): 实现传送器

This commit is contained in:
2025-10-23 09:26:47 +08:00
parent b9da52a61c
commit 5116f20b89
6 changed files with 398 additions and 1 deletions

View File

@@ -0,0 +1,83 @@
using System;
using UnityEngine;
namespace Script.Gameplay.Facility
{
public class TransportController : BaseFacilityController
{
[Header("传送器设置")]
[SerializeField] private Transform destinationPoint;
[SerializeField] private string playerTag = "Player";
[SerializeField] private float transportDelay = 0.1f;
[Header("检测区域参数")]
[SerializeField] private float detectRadius = 1.0f;
[SerializeField] private float detectHeight = 2.0f;
[SerializeField] private LayerMask playerLayer;
private bool canTransport = false;
private bool isTransporting = false;
private Collider[] hits;
// 接收信号,决定是否允许传送
public override void OnSignalReceived(bool active, GameObject sender)
{
base.OnSignalReceived(active, sender);
canTransport = active;
}
// Update is called once per frame
private void FixedUpdate()
{
if (!isOpenInEditor || !canTransport || destinationPoint == null) return;
hits = Physics.OverlapCapsule(
GetCapsuleStart(), GetCapsuleEnd(), detectRadius, playerLayer);
}
private void Update()
{
if (!isOpenInEditor || !canTransport || destinationPoint == null || hits == null) return;
foreach (var hit in hits)
{
if (hit.CompareTag(playerTag) && !isTransporting)
{
StartCoroutine(TransportPlayer(hit.gameObject));
break;
}
}
}
private Vector3 GetCapsuleStart()
{
return transform.position + Vector3.up * 0.1f;
}
private Vector3 GetCapsuleEnd()
{
return transform.position + Vector3.up * detectHeight;
}
private System.Collections.IEnumerator TransportPlayer(GameObject player)
{
isTransporting = true;
yield return new WaitForSeconds(transportDelay);
player.transform.position = destinationPoint.position;
isTransporting = false;
}
#if UNITY_EDITOR
private void OnDrawGizmosSelected()
{
Gizmos.color = Color.cyan;
Vector3 start = GetCapsuleStart();
Vector3 end = GetCapsuleEnd();
Gizmos.DrawWireSphere(start, detectRadius);
Gizmos.DrawWireSphere(end, detectRadius);
Gizmos.DrawLine(start + Vector3.forward * detectRadius, end + Vector3.forward * detectRadius);
Gizmos.DrawLine(start - Vector3.forward * detectRadius, end - Vector3.forward * detectRadius);
Gizmos.DrawLine(start + Vector3.right * detectRadius, end + Vector3.right * detectRadius);
Gizmos.DrawLine(start - Vector3.right * detectRadius, end - Vector3.right * detectRadius);
}
#endif
}
}