Legacy Documentation: Version 4.6(go to latest)
Language: English
  • C#
  • JS
  • Boo

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

Sumbission failed

For some reason your suggested change could not be submitted. Please try again 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, distance: float = Mathf.Infinity, layerMask: int = DefaultRaycastLayers): RaycastHit[];
public static RaycastHit[] RaycastAll(Ray ray, float distance = Mathf.Infinity, int layerMask = DefaultRaycastLayers);
public static def RaycastAll(ray as Ray, distance as float = Mathf.Infinity, layerMask as int = DefaultRaycastLayers) as RaycastHit[]
public static function RaycastAll(origin: Vector3, direction: Vector3, distance: float = Mathf.Infinity, layermask: int = DefaultRaycastLayers): RaycastHit[];
public static RaycastHit[] RaycastAll(Vector3 origin, Vector3 direction, float distance = Mathf.Infinity, int layermask = DefaultRaycastLayers);
public static def RaycastAll(origin as Vector3, direction as Vector3, distance as float = Mathf.Infinity, layermask as int = DefaultRaycastLayers) as RaycastHit[]

Description

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

	function Update () {
		var hits : RaycastHit[];
		hits = Physics.RaycastAll (transform.position, transform.forward, 100.0);

// Change the material of all hit colliders // to use a transparent Shader for (var i = 0;i < hits.Length; i++) { var hit : RaycastHit = hits[i]; var renderer = hit.collider.renderer; if (renderer) { renderer.material.shader = Shader.Find("Transparent/Diffuse"); renderer.material.color.a = 0.3; } } }
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    void Update() {
        RaycastHit[] hits;
        hits = Physics.RaycastAll(transform.position, transform.forward, 100.0F);
        int i = 0;
        while (i < hits.Length) {
            RaycastHit hit = hits[i];
            Renderer renderer = hit.collider.renderer;
            if (renderer) {
                renderer.material.shader = Shader.Find("Transparent/Diffuse");
                renderer.material.color.a = 0.3F;
            }
            i++;
        }
    }
}
import UnityEngine
import System.Collections

public class ExampleClass(MonoBehaviour):

	def Update() as void:
		hits as (RaycastHit)
		hits = Physics.RaycastAll(transform.position, transform.forward, 100.0F)
		i as int = 0
		while i < hits.Length:
			hit as RaycastHit = hits[i]
			renderer as Renderer = hit.collider.renderer
			if renderer:
				renderer.material.shader = Shader.Find('Transparent/Diffuse')
				renderer.material.color.a = 0.3F
			i++

Notes: This function will return false if you cast a ray from inside a sphere to the outside; this in an intended behaviour. If you move colliders from scripting or by animation, there needs to be at least one FixedUpdate executed so that the physics library can update it's data structures, before a Raycast will hit the collider at it's new position.