64 lines
1.8 KiB
C#
64 lines
1.8 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
/// <summary>
|
|
/// 边缘检测函数//未完成//TODO:
|
|
/// </summary>
|
|
namespace ZXK.GYJQR
|
|
{
|
|
public class BoundTrigger : MonoBehaviour
|
|
{
|
|
[Range(0.0f, 0.5f)]
|
|
[System.NonSerialized]
|
|
public float edgeRadius = 0.1f;//边缘检测的半径
|
|
public UnityEvent onNearEdge;//当前处于碰撞检测的范围内
|
|
public UnityEvent onOutEdge;//当前处于碰撞检测的范围外
|
|
public void Start()
|
|
{
|
|
enabled = false;
|
|
}
|
|
/// <summary>
|
|
/// 检测是否与边缘层交互
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public bool OnNearEdge(string tagName)
|
|
{
|
|
Collider collider = null;
|
|
var screenPos = Input.mousePosition;
|
|
var worldPos = transform.position;
|
|
// 摄像机的世界坐标
|
|
var camPos = Camera.main.transform.position;
|
|
// 投射长度
|
|
var lineLen = 1000;
|
|
RaycastHit[] hits = Physics.SphereCastAll(worldPos, edgeRadius, camPos - worldPos, lineLen);
|
|
if (hits.Length > 0)
|
|
{
|
|
for (int i = 0; i < hits.Length; i++)
|
|
{
|
|
if (hits[i].collider.tag.Equals(tagName))
|
|
{
|
|
collider = hits[i].collider;
|
|
}
|
|
}
|
|
}
|
|
return collider != null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取到进入碰撞检测的碰撞体
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public List<Collider> GetNearEdgeCollider()
|
|
{
|
|
return Physics.OverlapSphere(transform.position, edgeRadius).ToList();
|
|
}
|
|
#if UNITY_EDITOR
|
|
private void OnDrawGizmos()
|
|
{
|
|
Gizmos.DrawWireSphere(transform.position, edgeRadius);
|
|
}
|
|
#endif
|
|
}
|
|
}
|