public static RaycastHit2D Raycast (Vector2 origin, Vector2 direction, float distance= Mathf.Infinity, int layerMask= DefaultRaycastLayers, float minDepth= -Mathf.Infinity, float maxDepth= Mathf.Infinity);

Parameters

origin射线在 2D 空间中的起点。
directionA vector representing the direction of the ray.
distanceThe maximum distance over which to cast the ray.
layerMask过滤器,用于仅在特定层上检测碰撞体。
minDepth仅包括 Z 坐标(深度)大于或等于该值的对象。
maxDepth仅包括 Z 坐标(深度)小于或等于该值的对象。

Returns

RaycastHit2D 返回的投射数量。

Description

向场景中的碰撞体投射射线。

从概念上说,射线投射 类似于从空间中的某个点朝特定方向发射一条光束。在该过程中,可以检测并报告与光束接触的任何对象。

函数返回一个 RaycastHit 对象,该对象引用了射线命中的碰撞体(如果未命中任何对象,则结果的碰撞体属性将为 NULL)。layerMask 可用于仅在特定层上有选择地检测对象(例如,这让您能够仅将检测应用于敌人角色)。

使用 contactFilter 的此方法重载可以按 ContactFilter2D 中提供的选项筛选结果。

对于确定视线、炮火击中的目标以及游戏中的许多其他目的来说,射线投射很有用。

Additionally, this will also detect Collider(s) at the start of the ray. In this case, the ray starts inside the Collider and doesn't intersect the Collider surface. This means that the collision normal cannot be calculated, in which case the returned collision normal 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.

另请参阅:LayerMask 类、RaycastHit2D 类、RaycastAllLinecastDefaultRaycastLayersIgnoreRaycastLayerraycastsHitTriggers

using UnityEngine;

public class Example : MonoBehaviour { // Float a rigidbody object a set distance above a surface.

public float floatHeight; // Desired floating height. public float liftForce; // Force to apply when lifting the rigidbody. public float damping; // Force reduction proportional to speed (reduces bouncing).

Rigidbody2D rb2D;

void Start() { rb2D = GetComponent<Rigidbody2D>(); }

void FixedUpdate() { // Cast a ray straight down. RaycastHit2D hit = Physics2D.Raycast(transform.position, -Vector2.up);

// If it hits something... if (hit.collider != null) { // Calculate the distance from the surface and the "error" relative // to the floating height. float distance = Mathf.Abs(hit.point.y - transform.position.y); float heightError = floatHeight - distance;

// The force is proportional to the height error, but we remove a part of it // according to the object's speed. float force = liftForce * heightError - rb2D.velocity.y * damping;

// Apply the force to the rigidbody. rb2D.AddForce(Vector3.up * force); } } }

public static int Raycast (Vector2 origin, Vector2 direction, ContactFilter2D contactFilter, RaycastHit2D[] results, float distance= Mathf.Infinity);

Parameters

origin射线在 2D 空间中的起点。
directionA vector representing the direction of the ray.
contactFilter接触筛选器,用于以不同方式筛选结果,例如按层遮罩、Z 深度或法线角度。
results用于接收结果的数组。该数组的大小决定可返回的结果的最大数量。
distanceThe maximum distance over which to cast the ray.

Returns

int 返回放置在 results 数组中的结果数。

Description

向场景中的碰撞体投射射线。

从概念上说,射线投射 类似于从空间中的某个点朝特定方向发射一条光束。在该过程中,可以检测并报告与光束接触的任何对象。

该函数返回找到的接触点数,并将这些接触点放入 results 数组。也可以通过 contactFilter 对结果进行过滤。

另请参阅:ContactFilter2DRaycastHit2D


Parameters

origin射线在 2D 空间中的起点。
directionA vector representing the direction of the ray.
contactFilter接触筛选器,用于以不同方式筛选结果,例如按层遮罩、Z 深度或法线角度。
resultsThe list to receive results.
distanceThe maximum distance over which to cast the ray.

Returns

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

Description

Casts a ray against Colliders in the Scene.

A raycast is conceptually like a laser beam that is fired from a point in space along a particular direction. Any Collider making contact with the beam can be detected and reported.

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.

另请参阅:ContactFilter2DRaycastHit2D