Version: 2017.2

Collider.ClosestPoint

切换到手册
public Vector3 ClosestPoint (Vector3 position);

参数

position 您需要找到最接近点所对应的位置。

返回

Vector3 碰撞体上最接近指定位置的点。

描述

返回碰撞体上最接近给定位置的一个点。

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); } }
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961