Legacy Documentation: Version 2017.1 (Go to current version)
LanguageEnglish
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

Physics.RaycastAll

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

public static function RaycastAll(ray: Ray, maxDistance: float = Mathf.Infinity, layerMask: int = DefaultRaycastLayers, queryTriggerInteraction: QueryTriggerInteraction = QueryTriggerInteraction.UseGlobal): RaycastHit[];
public static RaycastHit[] RaycastAll(Ray ray, float maxDistance = Mathf.Infinity, int layerMask = DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal);

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 ignore colliders when casting a ray.
queryTriggerInteraction Specifies whether this query should hit Triggers.

Description

Casts a ray through the scene and returns all hits. Note that order is not guaranteed.

See Also: Raycast.

#pragma strict
function Update() {
	var hits: RaycastHit[];
	hits = Physics.RaycastAll(transform.position, transform.forward, 100.0F);
	for (var i: int = 0; i < hits.Length; i++) {
		var hit: RaycastHit = hits[i];
		var rend: Renderer = hit.transform.GetComponent.<Renderer>();
		if (rend) {
			// to use a transparent shader.
			rend.material.shader = Shader.Find("Transparent/Diffuse");
			var tempColor: Color = rend.material.color;
			tempColor.a = 0.3F;
			rend.material.color = tempColor;
		}
	}
}
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.


public static function RaycastAll(origin: Vector3, direction: Vector3, maxDistance: float = Mathf.Infinity, layermask: int = DefaultRaycastLayers, queryTriggerInteraction: QueryTriggerInteraction = QueryTriggerInteraction.UseGlobal): RaycastHit[];
public static RaycastHit[] RaycastAll(Vector3 origin, Vector3 direction, float maxDistance = Mathf.Infinity, int layermask = DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal);

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 ignore colliders when casting a ray.
queryTriggerInteraction Specifies whether this query should hit Triggers.

Description

See Also: Raycast.

See example above.

Did you find this page useful? Please give it a rating: