Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


RaycastAll

Additional Resources: Physics.Raycast.
Read time 2 minutesLast updated 6 days ago

Definition

  • Type: Method
  • Namespace: UnityEngine
  • Assembly: UnityEngine.PhysicsModule

RaycastAll(Vector3, Vector3, float, int, QueryTriggerInteraction)

Additional Resources: Physics.Raycast.
public static RaycastHit[] RaycastAll(Vector3 origin, Vector3 direction, float maxDistance, int layerMask, QueryTriggerInteraction queryTriggerInteraction)

Parameters

origin

The starting point of the ray in world coordinates.

direction

The direction of the ray.

maxDistance

The max distance the rayhit is allowed to be from the start of the ray.

layerMask

A Layer mask that is used to selectively filter which colliders are considered when casting a ray.

queryTriggerInteraction

Specifies whether this query should hit Triggers.

Returns

Type

Description

RaycastHit[]

Remarks

See example above.

RaycastAll(Ray, float, int, QueryTriggerInteraction)

Casts a ray through the Scene and returns all hits. Note that order of the results is undefined.
public static RaycastHit[] RaycastAll(Ray ray, float maxDistance, int layerMask, QueryTriggerInteraction queryTriggerInteraction)

Parameters

ray

The starting point and direction of the ray.

maxDistance

The max distance the rayhit is allowed to be from the start of the ray.

layerMask

A Layer mask that is used to selectively filter which colliders are considered when casting a ray.

queryTriggerInteraction

Specifies whether this query should hit Triggers.

Returns

Type

Description

RaycastHit[]An array of RaycastHit objects. Note that the order of the results is undefined.

Remarks

Additional Resources: Physics.Raycast.
using UnityEngine;using System.Collections;public class ExampleClass : MonoBehaviour{ void Update() { RaycastHit[] hits; hits = Physics.RaycastAll(transform.position, transform.forward, 100.0F); for (int i = 0; i < hits.Length; i++) { RaycastHit hit = hits[i]; Renderer rend = hit.transform.GetComponent<Renderer>(); if (rend) { // Change the material of all hit colliders // to use a transparent shader. rend.material.shader = Shader.Find("Transparent/Diffuse"); Color tempColor = rend.material.color; tempColor.a = 0.3F; rend.material.color = tempColor; } } }}
Notes: Raycasts will not detect colliders for which the raycast origin is inside the collider.