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
public static function
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);
public static function
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);
public static function
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);
public static function
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 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. |
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!");
}
}
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
void Update() {
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.
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 its data structures, before a Raycast will hit the collider at its new position.
public static function
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);
public static function
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);
public static function
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);
public static function
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 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. |
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;
}
RaycastHit hit;
float distanceToGround = 0;
if (Physics.Raycast(transform.position, -Vector3.up, out hit)) {
distanceToGround = hit.distance;
}
// Raycast up to 100 meters down
var hit : RaycastHit;
if (Physics.Raycast (transform.position, -Vector3.up, hit, 100.0)) {
var distanceToGround = hit.distance;
}
RaycastHit hit;
float distanceToGround = 0;
if (Physics.Raycast(transform.position, -Vector3.up, out hit, 100.0F)) {
distanceToGround = hit.distance;
}
public static function
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);
public static function
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);
public static function
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);
public static function
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 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. |
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
.
public static function
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);
public static function
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);
public static function
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);
public static function
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 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. |
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
.