Version: 2017.3 (switch to 2017.4)
LanguageEnglish
  • C#
  • JS

Script language

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

Physics.Raycast

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 method Raycast(origin: Vector3, direction: Vector3, maxDistance: float = Mathf.Infinity, layerMask: int = DefaultRaycastLayers, queryTriggerInteraction: QueryTriggerInteraction = QueryTriggerInteraction.UseGlobal): bool;
public static bool Raycast(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 ray should check for collisions.
layerMask A Layer mask that is used to selectively ignore Colliders when casting a ray.
queryTriggerInteraction Specifies whether this query should hit Triggers.

Returns

bool True if the ray intersects with a Collider, otherwise false.

Description

Casts a ray, from point origin, in direction direction, of length maxDistance, against all colliders in the scene.

You may optionally provide a LayerMask, to filter out any Colliders you aren't interested in generating collisions with.

Specifying queryTriggerInteraction allows you to control whether or not Trigger colliders generate a hit, or whether to use the global Physics.queriesHitTriggers setting.

This example creates a simple Raycast, projecting forwards from the position of the object's current position, extending for 10 units.

no example available in JavaScript
using UnityEngine;

public class ExampleClass : MonoBehaviour { // C# example. void Update() { // Bit shift the index of the layer (8) to get a bit mask int layerMask = 1 << 8;

// This would cast rays only against colliders in layer 8. // But instead we want to collide against everything except layer 8. The ~ operator does this, it inverts a bitmask. layerMask = ~layerMask;

RaycastHit hit; // Does the ray intersect any objects excluding the player layer if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit, Mathf.Infinity, layerMask)) { Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * hit.distance, Color.yellow); Debug.Log("Did Hit"); } else { Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * 1000, Color.white); Debug.Log("Did not Hit"); } } }
#pragma strict
function FixedUpdate() {
	var fwd: Vector3 = transform.TransformDirection(Vector3.forward);
	if (Physics.Raycast(transform.position, fwd, 10))
		print("There is something in front of the object!");
}
using UnityEngine;

public class ExampleClass : MonoBehaviour { void FixedUpdate() { Vector3 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.


public static method Raycast(origin: Vector3, direction: Vector3, out hitInfo: RaycastHit, maxDistance: float = Mathf.Infinity, layerMask: int = DefaultRaycastLayers, queryTriggerInteraction: QueryTriggerInteraction = QueryTriggerInteraction.UseGlobal): bool;
public static bool Raycast(Vector3 origin, Vector3 direction, out RaycastHit hitInfo, 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.
hitInfo If true is returned, hitInfo will contain more information about where the collider was hit. (See Also: RaycastHit).
maxDistance The max distance the ray should check for collisions.
layerMask A Layer mask that is used to selectively ignore colliders when casting a ray.
queryTriggerInteraction Specifies whether this query should hit Triggers.

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.

This example reports the distance between the current object and the reported Collider:

#pragma strict
public class RaycastExample extends MonoBehaviour {
	function FixedUpdate() {
		var hit: RaycastHit;
		if (Physics.Raycast(transform.position, -Vector3.up, hit))
			print("Found an object - distance: " + hit.distance);
	}
}
using UnityEngine;

public class RaycastExample : MonoBehaviour { void FixedUpdate() { RaycastHit hit;

if (Physics.Raycast(transform.position, -Vector3.up, out hit)) print("Found an object - distance: " + hit.distance); } }

This example re-introduces the maxDistance parameter to limit how far ahead to cast the Ray:

#pragma strict
public class RaycastExample extends MonoBehaviour {
	function FixedUpdate() {
		var hit: RaycastHit;
		if (Physics.Raycast(transform.position, -Vector3.up, hit, 100.0f))
			print("Found an object - distance: " + hit.distance);
	}
}
using UnityEngine;

public class RaycastExample : MonoBehaviour { void FixedUpdate() { RaycastHit hit;

if (Physics.Raycast(transform.position, -Vector3.up, out hit, 100.0f)) print("Found an object - distance: " + hit.distance); } }

public static method Raycast(ray: Ray, maxDistance: float = Mathf.Infinity, layerMask: int = DefaultRaycastLayers, queryTriggerInteraction: QueryTriggerInteraction = QueryTriggerInteraction.UseGlobal): bool;
public static bool Raycast(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 ray should check for collisions.
layerMask A Layer mask that is used to selectively ignore colliders when casting a ray.
queryTriggerInteraction Specifies whether this query should hit Triggers.

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.

#pragma strict
function Update() {
	var ray: Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
	if (Physics.Raycast(ray, 100))
		print("Hit something!");
}
using UnityEngine;

public class ExampleClass : MonoBehaviour { void Update() { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, 100)) print("Hit something!"); } }

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

Parameters

ray The starting point and direction of the ray.
hitInfo If true is returned, hitInfo will contain more information about where the collider was hit. (See Also: RaycastHit).
maxDistance The max distance the ray should check for collisions.
layerMask A Layer mask that is used to selectively ignore colliders when casting a ray.
queryTriggerInteraction Specifies whether this query should hit Triggers.

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.

This example draws a line along the length of the Ray whenever a collision is detected:

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

public class ExampleClass : MonoBehaviour { void Update() { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit;

if (Physics.Raycast(ray, out hit, 100)) Debug.DrawLine(ray.origin, hit.point); } }

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