Physics.Raycast
static function Raycast (origin : Vector3, direction : Vector3, distance : float = Mathf.Infinity, layerMask : int = kDefaultRaycastLayers) : bool
Parameters
Name | Description |
origin |
The starting point of the ray in world coordinates.
|
direction |
The direction of the ray.
|
distance |
The length of the ray
|
layerMask |
A 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!");
}
}
using UnityEngine;
using System.Collections;
public class example :
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!");
}
}
import UnityEngine
import System.Collections
class example(
MonoBehaviour):
def
Update():
fwd as
Vector3 = transform.TransformDirection(
Vector3.forward)
if Physics.Raycast(transform.position, fwd, 10):
print('There is something
in front of the object!')
Note: This function will return false if you cast a ray from
inside a sphere to the outside; this in an intended behaviour.
static function Raycast (origin : Vector3, direction : Vector3, out hitInfo : RaycastHit, distance : float = Mathf.Infinity, layerMask : int = kDefaultRaycastLayers) : bool
Parameters
Name | Description |
origin |
The starting point of the ray in world coordinates.
|
direction |
The direction of the ray.
|
distance |
The length of the ray
|
hitInfo |
If true is returned, hitInfo will contain more information about where the collider was hit (See Also: RaycastHit).
|
layerMask |
A 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.
function Update () {
var hit :
RaycastHit;
if (
Physics.Raycast (transform.position, -Vector3.up, hit)) {
var distanceToGround = hit.distance;
}
}
using UnityEngine;
using System.Collections;
public class example :
MonoBehaviour {
void Update() {
RaycastHit hit;
if (
Physics.Raycast(transform.position, -Vector3.up, out hit))
float distanceToGround = hit.distance;
}
}
import UnityEngine
import System.Collections
class example(
MonoBehaviour):
def
Update():
hit as
RaycastHit if Physics.Raycast(transform.position, -Vector3.up, hit):
distanceToGround as single = hit.distance
another example:
function Update () {
var hit :
RaycastHit;
if (
Physics.Raycast (transform.position, -Vector3.up, hit, 100.0)) {
var distanceToGround = hit.distance;
}
}
using UnityEngine;
using System.Collections;
public class example :
MonoBehaviour {
void Update() {
RaycastHit hit;
if (
Physics.Raycast(transform.position, -Vector3.up, out hit, 100.0F))
float distanceToGround = hit.distance;
}
}
import UnityEngine
import System.Collections
class example(
MonoBehaviour):
def
Update():
hit as
RaycastHit if Physics.Raycast(transform.position, -Vector3.up, hit, 100.0F):
distanceToGround as single = hit.distance
static function Raycast (ray : Ray, distance : float = Mathf.Infinity, layerMask : int = kDefaultRaycastLayers) : bool
Parameters
Name | Description |
ray |
The starting point and direction of the ray.
|
distance |
The length of the ray
|
layerMask |
A 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.
static function Raycast (ray : Ray, out hitInfo : RaycastHit, distance : float = Mathf.Infinity, layerMask : int = kDefaultRaycastLayers) : bool
Parameters
Name | Description |
ray |
The starting point and direction of the ray.
|
distance |
The length of the ray
|
hitInfo |
If true is returned, hitInfo will contain more information about where the collider was hit (See Also: RaycastHit).
|
layerMask |
A 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.