# Physics.Raycast 光线投射

static function Raycast (origin : Vector3, direction : Vector3, distance : float = Mathf.Infinity, layerMask : int = kDefaultRaycastLayers) : bool

### Parameters参数

• origin
The starting point of the ray in world coordinates.
在世界坐标，射线的起始点。
• direction
The direction of the ray.
射线的方向。
• distance
The length of the ray
射线的长度。
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.

``````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!");

}
}``````
``````function Update () {
var fwd = 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参数

• origin
The starting point of the ray in world coordinates.
在世界坐标，射线的起始点。
• direction
The direction of the ray.
射线的方向。
• distance
The length of the ray
射线的长度。
• hitInfo
如果返回true，hitInfo将包含碰到器碰撞的更多信息。
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.

``````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;

}
}``````
``````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, 100.0F))
float distanceToGround = hit.distance;

}
}``````
``````// Raycast up to 100 meters forward
//光线向前投射100米远
function Update () {
var hit : RaycastHit;
if (Physics.Raycast (transform.position, -Vector3.up, hit, 100.0)) {
var distanceToGround = hit.distance;
}
}``````

• static function Raycast (ray : Ray, distance : float = Mathf.Infinity, layerMask : int = kDefaultRaycastLayers) : bool

### Parameters参数

• ray
The starting point and direction of the ray.
射线的起点和方向
• distance
The length of the ray
射线的长度。
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.

``````using UnityEngine;
using System.Collections;

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

}
}``````
``````var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, 100)) {
print ("Hit something");
}``````

• static function Raycast (ray : Ray, out hitInfo : RaycastHit, distance : float = Mathf.Infinity, layerMask : int = kDefaultRaycastLayers) : bool

### Parameters参数

• ray
The starting point and direction of the ray.
射线的起点和方向
• distance
The length of the ray
射线的长度
• hitInfo
如果返回true，hitInfo将包含碰到器碰撞的更多信息。
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.

``````using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
public Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
public RaycastHit hit;
void Awake() {
if (Physics.Raycast(ray, out hit, 100))
Debug.DrawLine(ray.origin, hit.point);

}
}``````
``````var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast (ray, hit, 100)) {
Debug.DrawLine (ray.origin, hit.point);
}``````
Page last updated: 2011-4-9