52 lines
1.6 KiB
C#
52 lines
1.6 KiB
C#
using UnityEngine;
|
|
|
|
public class MeasureMeshDistance : MonoBehaviour
|
|
{
|
|
public Transform target; // 타겟 오브젝트
|
|
public Transform referencePoint; // 비교할 지점(예: 바닥 또는 다른 오브젝트의 위치)
|
|
|
|
private MeshFilter meshFilter;
|
|
private Mesh mesh;
|
|
|
|
void Start()
|
|
{
|
|
if (target == null || referencePoint == null)
|
|
{
|
|
Debug.LogError("Target or Reference Point not assigned.");
|
|
return;
|
|
}
|
|
|
|
meshFilter = target.GetComponent<MeshFilter>();
|
|
if (meshFilter == null)
|
|
{
|
|
Debug.LogError("Target does not have a MeshFilter component.");
|
|
return;
|
|
}
|
|
|
|
mesh = meshFilter.mesh;
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
if (mesh == null) return;
|
|
|
|
float minDistance = float.MaxValue;
|
|
Vector3 closestPoint = Vector3.zero;
|
|
|
|
// 메시의 모든 버텍스들을 순회하며 가장 가까운 거리를 계산
|
|
foreach (Vector3 vertex in mesh.vertices)
|
|
{
|
|
Vector3 worldVertex = target.TransformPoint(vertex); // 로컬 공간을 월드 공간으로 변환
|
|
float distance = Vector3.Distance(worldVertex, referencePoint.position);
|
|
|
|
if (distance < minDistance)
|
|
{
|
|
minDistance = distance;
|
|
closestPoint = worldVertex;
|
|
}
|
|
}
|
|
|
|
Debug.Log("Closest Distance to Reference Point: " + minDistance);
|
|
Debug.DrawLine(referencePoint.position, closestPoint, Color.red); // Scene 뷰에서 확인을 위한 시각적 표시
|
|
}
|
|
} |