返回介绍

Physics2D.Linecast 线投射

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

JavaScript => static function Linecast(start: Vector2, end: Vector2, layerMask: int = DefaultRaycastLayers, minDepth: float = -Mathf.Infinity, maxDepth: float = Mathf.Infinity): RaycastHit2D;
C# => static RaycastHit2D Linecast(Vector2 start, Vector2 end, int layerMask = DefaultRaycastLayers, float minDepth = -Mathf.Infinity, float maxDepth = Mathf.Infinity);

Parameters 参数

startThe start point of the line in world space.
线在世界空间的开始点。
endThe end point of the line in world space.
线在时间空间的结束点。
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 返回值

RaycastHit2D The cast results returned.
RaycastHit2D 类型的投射返回结果。

Description 描述

Casts a line against colliders in the scene.
对场景内的碰撞器们投射出一条线。

A linecast is an imaginary line between two points in world space. Any object making contact with the beam can be detected and reported. This differs from the similar raycast in that raycasting specifies the line using an origin and directtion.
直线投射是一条在世界空间中两点之间的虚拟线。任何对象接触到光束都会被检测到和被报告。这个与类似的光线投射不同的是光线投射出来的线是基于原点和方向的。

This function returns a RaycastHit2D object when the line contacts a Collider in the scene. The layerMask can be used to detect objects selectively only on certain layers (this allows you to apply the detection only to enemy characters, for example).
当直线接触到场景中的碰撞器的时候这个函数会返回一个RaycastHit2D对象。层遮罩会被用来有选择性地只在指定的层中检测各种对象(比如:这允许您设置检测只适用于敌人角色)

The direction of the line is assumed to extend from the start point to the end point. Only the first collider encountered in that direction will be reported.
直线的方向是假设冲起点到终点。在这个方向上,只有第一个被接触到的碰撞器会被报告。

Although the Z axis is not relevant for rendering or collisions in 2D, you can use the minDepth and maxDepth parameters to filter objects based on their Z coordinate. Linecasts are useful for determining lines of sight, targets hit by gunfire and for many other purposes in gameplay.
虽然Z轴与2D的渲染或碰撞无关,但是你可以使用minDepth和maxDepth参数去排除一些对象基于他们的Z轴。直线投射有助于确定视线(瞄准线),目标被枪击中和有助于实现许多其他游戏目的。

Note that this function will allocate memory for the returned RaycastHit2D object. You can use LinecastNonAlloc to avoid this overhead if you need to make linecasts frequently.
注意这个函数会分配内存给返回的RaycastHit2D对象。如果你需要频繁做这个检测的时候,你可以使用LinecastNonAlloc去避免这些开销。

Additionally, this will also detect Collider(s) at the start of the line. In this case the line 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 line 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, LinecastAll, LinecastNonAlloc, DefaultRaycastLayers, IgnoreRaycastLayer, raycastsHitTriggers.

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

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

发布评论

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