Legacy Documentation: Version 5.1
LanguageEnglish
  • C#
  • JS

Script language

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

Physics.RaycastAll

Switch to Manual
public static function RaycastAll(ray: Ray, maxDistance: float = Mathf.Infinity, layerMask: int = DefaultRaycastLayers): RaycastHit[];
public static function RaycastAll(origin: Vector3, direction: Vector3, maxDistance: float = Mathf.Infinity, layermask: int = DefaultRaycastLayers): RaycastHit[];

Parameters

Description

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

#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;
		}
	}
}

Notes: Raycasts will not detect colliders for which the raycast origin is inside the collider. 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.