Version: 2017.1
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 @param origin Стартовая точка фигуры в 2D пространстве.
direction The vector representing the direction of the ray.
distance Maximum distance over which to cast the ray.
layerMask @param layerMask Фильтр для обнаружения коллайдеров только на определённых слоях.
minDepth @param minDepth Включает только объекты с координатой Z (глубиной) выше, чем это значение.
maxDepth @param maxDepth Включает только объекты с координатой Z (глубиной) меньше, чем это значение.

Returns

RaycastHit2D The cast results returned.

Description

Casts a ray against colliders in the scene.

BoxCast, концептуально, это как протягивание коробки сквозь сцену в определенном направлении. Любой объект, вступающий в контакт с коробкой, может быть обнаружен и зарегистрирован.

This function returns a RaycastHit object with a reference to the collider that is hit by the ray (the collider property of the result will be NULL if nothing was hit). 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).

Overloads of this method that use contactFilter can filter the results by the options available in ContactFilter2D.

Raycasts are useful for determining lines of sight, targets hit by gunfire and for many other purposes in gameplay.

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.

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

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour { public float floatHeight; public float liftForce; public float damping; public Rigidbody2D rb2D; void Start() { rb2D = GetComponent<Rigidbody2D>(); } void FixedUpdate() { RaycastHit2D hit = Physics2D.Raycast(transform.position, -Vector2.up); if (hit.collider != null) { float distance = Mathf.Abs(hit.point.y - transform.position.y); float heightError = floatHeight - distance; float force = liftForce * heightError - rb2D.velocity.y * damping; rb2D.AddForce(Vector3.up * force); } } }

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

Parameters

origin @param origin Стартовая точка фигуры в 2D пространстве.
direction The vector representing the direction of the ray.
contactFilter The contact filter used to filter the results differently, such as by layer mask, Z depth, or normal angle.
results The array to receive results. The size of the array determines the maximum number of results that can be returned.
distance Maximum distance over which to cast the ray.

Returns

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

Description

Casts a ray against colliders in the Scene.

BoxCast, концептуально, это как протягивание коробки сквозь сцену в определенном направлении. Любой объект, вступающий в контакт с коробкой, может быть обнаружен и зарегистрирован.

This function returns the number of contacts found and places those contacts in the results array. The results can also be filtered by the contactFilter.

See Also: ContactFilter2D and RaycastHit2D.