Physics.Raycast 光线投射

static function Raycast (origin : 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.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.

注意:如果从一个球型体的内部到外部用光线投射,返回为假。