Legacy Documentation: Version 5.0
Language: English
  • C#
  • JS

Script language

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

Physics.Raycast

Switch to Manual
public static function Raycast(origin: Vector3, direction: Vector3, maxDistance: float = Mathf.Infinity, layerMask: int = DefaultRaycastLayers): bool;

Parameters

originThe starting point of the ray in world coordinates.
directionThe direction of the ray.
distanceThe length of the ray.
layerMaskA Layer mask that is used to selectively ignore colliders when casting a ray.

Returns

bool True when the ray intersects any collider, otherwise false.

Description

Casts a ray against all colliders in the scene.

	function Update () {
		var fwd = transform.TransformDirection (Vector3.forward);
		if (Physics.Raycast (transform.position, fwd, 10)) {
			print ("There is something in front of the object!");
		}
	}

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.


public static function Raycast(origin: Vector3, direction: Vector3, out hitInfo: RaycastHit, maxDistance: float = Mathf.Infinity, layerMask: int = DefaultRaycastLayers): bool;

Parameters

originThe starting point of the ray in world coordinates.
directionThe direction of the ray.
hitInfoIf true is returned, hitInfo will contain more information about where the collider was hit (See Also: RaycastHit).
distanceThe length of the ray.
layerMaskA Layer mask that is used to selectively ignore colliders when casting a ray.

Returns

bool True when the ray intersects any collider, otherwise false.

Description

Casts a ray against all colliders in the scene and returns detailed information on what was hit.

		var hit : RaycastHit;
		if (Physics.Raycast (transform.position, -Vector3.up, hit)) {
			var distanceToGround = hit.distance;
		}
	

Another example:

	// Raycast up to 100 meters down
		var hit : RaycastHit;
		if (Physics.Raycast (transform.position, -Vector3.up, hit, 100.0)) {
			var distanceToGround = hit.distance;
		}
	

public static function Raycast(ray: Ray, maxDistance: float = Mathf.Infinity, layerMask: int = DefaultRaycastLayers): bool;

Parameters

rayThe starting point and direction of the ray.
distanceThe length of the ray.
layerMaskA Layer mask that is used to selectively ignore colliders when casting a ray.

Returns

bool True when the ray intersects any collider, otherwise false.

Description

Same as above using ray.origin and ray.direction instead of origin and direction.

function Update() {
	var ray = Camera.main.ScreenPointToRay (Input.mousePosition);

if (Physics.Raycast (ray, 100)) { print ("Hit something"); } }

public static function Raycast(ray: Ray, out hitInfo: RaycastHit, maxDistance: float = Mathf.Infinity, layerMask: int = DefaultRaycastLayers): bool;

Parameters

rayThe starting point and direction of the ray.
hitInfoIf true is returned, hitInfo will contain more information about where the collider was hit (See Also: RaycastHit).
distanceThe length of the ray.
layerMaskA Layer mask that is used to selectively ignore colliders when casting a ray.

Returns

bool True when the ray intersects any collider, otherwise false.

Description

Same as above using ray.origin and ray.direction instead of origin and direction.

function Update() {
	var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
	var hit : RaycastHit;
	if (Physics.Raycast (ray, hit, 100)) {
		Debug.DrawLine (ray.origin, hit.point);
	}
}