UnityAPI手册-2019.4官网版
文集大纲加载中……
本文档使用 MrDoc 发布
-
+
首页
Collider.ClosestPoint
# [Collider](https://docs.unity3d.com/cn/2019.4/ScriptReference/Collider.html).ClosestPoint public [Vector3](https://docs.unity3d.com/cn/2019.4/ScriptReference/Vector3.html) **ClosestPoint** ([Vector3](https://docs.unity3d.com/cn/2019.4/ScriptReference/Vector3.html) **position**); ## 参数 | position | 您需要找到最接近点所对应的位置。 | | ---------- | ---------------------------------- | ## 返回 **Vector3** 碰撞体上最接近指定位置的点。 ## 描述 返回碰撞体上最接近给定位置的一个点。 此方法计算碰撞体上最接近于 3D 世界位置的点。在以下示例中,`closestPoint` 是碰撞体上的点,`location` 是 3D 空间中的点。如果 `location` 位于碰撞体中,`closestPoint` 将在内部。 **注意:** 与 [ClosestPointOnBounds](https://docs.unity3d.com/cn/2019.4/ScriptReference/Collider.ClosestPointOnBounds.html) 的区别是返回的点实际上位于碰撞体上,而不是在碰撞体的边界上。([bounds](https://docs.unity3d.com/cn/2019.4/ScriptReference/Collider-bounds.html) 是围绕碰撞体的框形。) ``` 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. // // 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](https://docs.unity3d.com/cn/2019.4/ScriptReference/Physics.ClosestPoint.html) 相同,但不允许传入自定义位置和旋转,而是使用碰撞体的位置。
da
2022年5月25日 10:32
转发文档
收藏文档
上一篇
下一篇
手机扫码
复制链接
手机扫一扫转发分享
复制链接
关于 MrDoc
觅思文档MrDoc
是
州的先生
开发并开源的在线文档系统,其适合作为个人和小型团队的云笔记、文档和知识库管理工具。
如果觅思文档给你或你的团队带来了帮助,欢迎对作者进行一些打赏捐助,这将有力支持作者持续投入精力更新和维护觅思文档,感谢你的捐助!
>>>捐助鸣谢列表
微信
支付宝
QQ
PayPal
Markdown文件
分享
链接
类型
密码
更新密码