Collider.ClosestPoint
Declaration
public Vector3 ClosestPoint(Vector3 position);Параметры
| Параметр | Описание |
|---|---|
| должность | Местоположение, к которому вы хотите найти ближайшую точку. |
Возвращаемое значение
Vector3 Ближайшая точка на коллайдере, возвращенная в мировых космических координатах.
Описание
Ближайшая точка на коллайдере в данном конкретном месте.
Этот метод вычисляет точку на Collider, которая находится ближе всего к 3D местоположению в мире. В примере ниже closestPoint — это точка на Collider и location — это точка в пространстве 3D. Если location находится в Collider, то closestPoint находится внутри. Если Collider отключен, метод возвращает вход position.
Примечание: Отличие от Collider.ClosestPointOnBounds в том, что возвращаемая точка фактически находится на коллайдере, а не на границах коллайдера. (bounds — это квадрат, окружающий коллайдер.)
using UnityEngine;
// Note that closestPoint is based on the surface of the collider // and location represents a point in 3d space. // The gizmos work in the editor. // Note that closestPoint functions correctly regardless of whether the collider is set as a trigger. // // Create an origin-based cube and give it a scale of (1, 0.5, 3). // Change the BoxCollider size to (0.8, 1.2, 0.8). This means that // collisions will happen when a GameObject gets close to the BoxCollider. // The ShowClosestPoint.cs script shows spheres that display the location // and closestPoint. Try changing the BoxCollider size and the location // values.
// Attach this to a GameObject that has a Collider component attached public class ShowClosestPoint : MonoBehaviour { public Vector3 location;
public void OnDrawGizmos() { var collider = GetComponent<Collider>();
if (!collider) { return; // nothing to do without a collider }
Vector3 closestPoint = collider.ClosestPoint(location);
Gizmos.DrawSphere(location, 0.1f); Gizmos.DrawWireSphere(closestPoint, 0.1f); } }
Примечание: То же самое, что Physics.ClosestPoint, но не позволяет передавать пользовательское положение и вращение. Вместо этого используется положение коллайдера.