Physics.Raycast 光线投射

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

Parameters参数

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参数

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参数

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.

使用ray.origin和ray.direction同上,替代origin和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参数

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.

使用ray.origin和ray.direction同上,替代origin和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);
}
最后修改:2011年4月9日 Saturday 12:06

本脚本参考基于Unity 3.4.1f5

英文部分版权属©Unity公司所有,中文部分© Unity圣典 版权所有,未经许可,严禁转载 。