返回介绍

Physics2D.RaycastNonAlloc 光线投射不分配

发布于 2019-12-18 15:38:17 字数 4310 浏览 1086 评论 0 收藏 0

JavaScript => static function RaycastNonAlloc(origin: Vector2, direction: Vector2, results: RaycastHit2D[], distance: float = Mathf.Infinity, layerMask: int = DefaultRaycastLayers, minDepth: float = -Mathf.Infinity, maxDepth: float = Mathf.Infinity): int;
C# => static int RaycastNonAlloc(Vector2 origin, Vector2 direction, RaycastHit2D[] results, float distance = Mathf.Infinity, int layerMask = DefaultRaycastLayers, float minDepth = -Mathf.Infinity, float maxDepth = Mathf.Infinity);

Parameters 参数

originThe point in 2D space where the ray originates.
光线在二维空间中的起点。
directionVector representing the direction of the ray.
光线的方向。
resultsArray to receive results.
接收结果的数组。
distanceMaximum distance over which to cast the ray.
光线的最大投射距离。
layerMaskFilter to detect Colliders only on certain layers.
只在某些层过滤检测碰撞器。
minDepthOnly include objects with a Z coordinate (depth) greater than this value.
只包括Z坐标(深度)大于这个值的对象。
maxDepthOnly include objects with a Z coordinate (depth) less than this value.
只包括Z坐标(深度)小于这个值的对象。

Returns 返回值

int The number of results returned.
int 返回结果的数量。

Description 描述

Casts a ray into the scene.
在场景中投射一条光线。

This function is similar to the RaycastAll function except that the results are returned in the supplied array. The integer return value is the number of objects that intersect the line (possibly zero) but the results array will not be resized if it doesn't contain enough elements to report all the results. The significance of this is that no memory is allocated for the results and so garbage collection performance is improved when raycasts are performed frequently.
这个函数与RaycastAll相似,区别在于他把返回结果放到一个事先准备好的数组内。整形的返回值(可能为零)就是光线贯穿的对象的数量,但是结果数组将不会重新调整大小即使他没有足够的元素装下所有的结果(碰撞器)。他的意义在于整个过程中不会为结果重新分配内存,使垃圾回收的频率降低从而提高了性能。

Additionally, this will also detect Collider(s) at the start of the ray. In this case the ray is starting inside the Collider and doesn't intersect the Collider surface. This means that the collision normal cannot be calculated in which case the collision normal returned is set to the inverse of the ray vector being tested. This can easily be detected because such results are always at a RaycastHit2D fraction of zero.
此外,这将会在光线的起点检测碰撞器(们)。在这种情况下,光线起点在碰撞器内而没有贯穿碰撞器的表面。这意味着在这种情况下,碰撞的法线不能被计算出来,而且碰撞法线将会与直线的方向相反。这能够被简单的查出来因为有问题的结果在RaycastHit2D中的法线部分是零。

See Also: LayerMask class, RaycastHit2D class, Raycast, Linecast, DefaultRaycastLayers, IgnoreRaycastLayer, raycastsHitTriggers.

Physics2D

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文