46 lines
1.6 KiB
C#
46 lines
1.6 KiB
C#
|
using System.Collections;
|
||
|
using System.Collections.Generic;
|
||
|
using UnityEngine;
|
||
|
namespace BITKit
|
||
|
{
|
||
|
public interface IClosePoint
|
||
|
{
|
||
|
bool TryGetClosePoint(out Vector3 vector3);
|
||
|
}
|
||
|
[System.Serializable]
|
||
|
public class GetClosePointFromCollider : IClosePoint
|
||
|
{
|
||
|
public Transform root;
|
||
|
public LayerMask layerMask;
|
||
|
public float distance;
|
||
|
public bool TryGetClosePoint(out Vector3 vector3)
|
||
|
{
|
||
|
vector3 = default;
|
||
|
if (UnityEngine.Physics.Raycast(root.position, root.forward, out var raycastHit, distance, layerMask))
|
||
|
{
|
||
|
var collider = raycastHit.collider;
|
||
|
switch (collider)
|
||
|
{
|
||
|
case MeshCollider meshCollider:
|
||
|
if (meshCollider.convex is false)
|
||
|
return false;
|
||
|
break;
|
||
|
}
|
||
|
vector3 = collider.ClosestPoint(root.position + Vector3.up);
|
||
|
var top = collider.bounds.center + collider.bounds.extents;
|
||
|
|
||
|
if (Mathf.Abs(vector3.y - top.y) > 0.16f) return false;
|
||
|
|
||
|
if (UnityEngine.Physics.Linecast(root.position, vector3, out var raycastHit2, layerMask))
|
||
|
{
|
||
|
if(raycastHit2.collider != collider)
|
||
|
return false;
|
||
|
};
|
||
|
return true;
|
||
|
//return vector3.y >= collider.bounds.center.y + collider.bounds.extents.y;
|
||
|
//return true;
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
}
|