Version: 2017.1
public Vector3 ClosestPoint (Vector3 position);

Parámetros

position Location you want to find the closest point to.

Valor de retorno

Vector3 The point on the collider that is closest to the specified location.

Descripción

Returns a point on the collider that is closest to a given location.

Same as ClosestPoint but doesn't allow passing a custom position and rotation. Instead, it uses the position and rotation of the collider.

Note that the difference from ClosestPointOnBounds is that the returned point is actually on the collider instead of on the bounds of the collider.

using UnityEngine;

// 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); } }