Version: 2019.1
public static RaycastHit2D Linecast (Vector2 start, Vector2 end, int layerMask= DefaultRaycastLayers, float minDepth= -Mathf.Infinity, float maxDepth= Mathf.Infinity);

Parameters

start@param point Точка в пространстве в мировых координатах.
end@param point Точка в пространстве в мировых координатах.
layerMask@param layerMask Фильтр для обнаружения коллайдеров только на определённых слоях.
minDepth@param minDepth Включает только объекты с координатой Z (глубиной) выше, чем это значение.
maxDepth@param maxDepth Включает только объекты с координатой Z (глубиной) меньше, чем это значение.

Returns

RaycastHit2D The cast results returned.

Description

Casts a line segment against colliders in the Scene.

A linecast is an imaginary line between two points in world space. Any object making contact with this line can be detected and reported. This differs from the similar raycast in that raycasting specifies the line using an origin and direction.

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). 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.

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.

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.

See Also: LayerMask class, RaycastHit2D class, LinecastAll, LinecastNonAlloc, DefaultRaycastLayers, IgnoreRaycastLayer, raycastsHitTriggers.


public static int Linecast (Vector2 start, Vector2 end, ContactFilter2D contactFilter, RaycastHit2D[] results);

Parameters

start@param point Точка в пространстве в мировых координатах.
end@param point Точка в пространстве в мировых координатах.
contactFilterThe contact filter used to filter the results differently, such as by layer mask, Z depth, or normal angle.
resultsThe array to receive results. The size of the array determines the maximum number of results that can be returned.

Returns

int Returns the number of results placed in the results array.

Description

Casts a line segment against colliders in the Scene with results filtered by ContactFilter2D.

A linecast is an imaginary line between two points in world space. Any object making contact with this line can be detected and reported. This differs from the similar raycast in that raycasting specifies the line using an origin and direction.

The overloads of this function with the contactFilter parameter can filter the returned results by the options in ContactFilter2D.

See Also: ContactFilter2D and RaycastHit2D.


Parameters

start@param point Точка в пространстве в мировых координатах.
end@param point Точка в пространстве в мировых координатах.
contactFilterThe contact filter used to filter the results differently, such as by layer mask, Z depth, or normal angle.
resultsThe list to receive results.

Returns

void Returns the number of results placed in the results list.

Description

Casts a line segment against Colliders in the Scene with results filtered by ContactFilter2D.

A linecast is an imaginary line between two points in world space. Any Collider making contact with this line can be detected and reported. This differs from the similar raycast in that raycasting specifies the line using an origin and direction.

The integer return value is the number of results written into the results list. The results list will be resized if it doesn't contain enough elements to report all the results. This prevents memory from being allocated for results when the results list does not need to be resized, and improves garbage collection performance when the query is performed frequently.

The results can also be filtered by the contactFilter.

See Also: ContactFilter2D and RaycastHit2D.

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