public bool Raycast (Vector3 origin, Vector3 direction, float maxDistance= Mathf.Infinity, int layerMask= Physics.DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction= QueryTriggerInteraction.UseGlobal);

Parameters

originThe starting point of the ray in world coordinates.
directionНаправление луча.
maxDistanceThe max distance the ray should check for collisions.
layerMaskA Layer mask that is used to selectively ignore Colliders when casting a ray.
queryTriggerInteractionSpecifies whether this query should hit Triggers.

Returns

bool @return True если луч пересекся хотя-бы с одним коллайдером,в ином случае - false.

Description

Casts a ray, from point origin, in direction direction, of length maxDistance, against all colliders in the Scene.

You may optionally provide a LayerMask, to filter out any Colliders you aren't interested in generating collisions with. Specifying queryTriggerInteraction allows you to control whether or not Trigger colliders generate a hit, or whether to use the global Physics.queriesHitTriggers setting.

This example creates a simple Raycast, projecting forwards from the position of the object's current position, extending for 10 units.

using UnityEngine;

public class ExampleClass : MonoBehaviour { void FixedUpdate() { Vector3 fwd = transform.TransformDirection(Vector3.forward);

if (Physics.Raycast(transform.position, fwd, 10)) print("There is something in front of the object!"); } }

public bool Raycast (Vector3 origin, Vector3 direction, out RaycastHit hitInfo, float maxDistance= Mathf.Infinity, int layerMask= Physics.DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction= QueryTriggerInteraction.UseGlobal);

Parameters

originThe starting point of the ray in world coordinates.
directionНаправление луча.
hitInfoIf true is returned, hitInfo will contain more information about where the collider was hit. (See Also: RaycastHit).
maxDistanceThe max distance the ray should check for collisions.
layerMaskA Layer mask that is used to selectively ignore Colliders when casting a ray.
queryTriggerInteractionSpecifies whether this query should hit Triggers.

Returns

bool @return True если луч пересекся хотя-бы с одним коллайдером,в ином случае - false.

Description

Casts a ray, from point origin, in direction direction, of length maxDistance, against all colliders in the Scene.

This method generates no garbage.

using UnityEngine;
public class RaycastExample : MonoBehaviour
{
    void FixedUpdate()
    {
        RaycastHit hit;

if (Physics.Raycast(transform.position, -Vector3.up, out hit)) print("Found an object - distance: " + hit.distance); } }

public int Raycast (Vector3 origin, Vector3 direction, RaycastHit[] raycastHits, float maxDistance= Mathf.Infinity, int layerMask= Physics.DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction= QueryTriggerInteraction.UseGlobal);

Parameters

origin@param ray Начальная точка и направление луча.
directionНаправление луча.
raycastHitsThe buffer to store the hits into.
maxDistanceThe max distance the rayhit is allowed to be from the start of the ray.
layerMaskA Layer mask that is used to selectively ignore colliders when casting a ray.
queryTriggerInteractionThe amount of hits stored into the results buffer.

Returns

int @return True если луч пересекся хотя-бы с одним коллайдером,в ином случае - false.

Description

Casts a ray, from point origin, in direction direction, of length maxDistance, against all colliders in the Scene.

This method generates no garbage.