Legacy Documentation: Version 2017.2 (Go to current version)
LanguageEnglish
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

Collider.ClosestPoint

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

public method ClosestPoint(position: Vector3): Vector3;
public Vector3 ClosestPoint(Vector3 position);

Parameters

position Location you want to find the closest point to.

Returns

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

Description

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.

#pragma strict
// Attach this to a GameObject that has a Collider component attached
public class ShowClosestPoint extends MonoBehaviour {
	public var location: Vector3;
	public function OnDrawGizmos() {
		var collider: var = GetComponent.<Collider>();
		if (!collider)return ;
		// nothing to do without a collider
		var closestPoint: Vector3 = collider.ClosestPoint(location);
		Gizmos.DrawSphere(location, 0.1f);
		Gizmos.DrawWireSphere(closestPoint, 0.1f);
	}
}
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); } }

Did you find this page useful? Please give it a rating: