Physics.RaycastAll 所有光线投射

static function RaycastAll (ray : Ray, distance : float = Mathf.Infinity, layerMask : int = kDefaultRaycastLayers) : RaycastHit[]
static function RaycastAll (origin : Vector3, direction : Vector3, distance : float = Mathf.Infinity, layermask : int = kDefaultRaycastLayers) : RaycastHit[]

Description描述

Casts a ray through the scene and returns all hits.

投射一条光线并返回所有碰撞,也就是投射光线并返回一个RaycastHit[]结构体。

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
	void Update() {
		RaycastHit[] hits;
		hits = Physics.RaycastAll(transform.position, transform.forward, 100.0F);
		int i = 0;
		while (i < hits.Length) {
			RaycastHit hit = hits[i];
			System.Object renderer = hit.collider.renderer;
			if (renderer) {
				renderer.material.shader = Shader.Find("Transparent/Diffuse");
				renderer.material.color.a = 0.3F;
			}
			i++;
		}
	}
}
function Update () {
	var hits : RaycastHit[];
	hits = Physics.RaycastAll (transform.position, transform.forward, 100.0);

	// Change the material of all hit colliders 
	// to use a transparent Shader
	//改变所有碰到碰撞器的材质,使用透明的着色器
	for (var i = 0;i < hits.Length; i++) {
		var hit : RaycastHit = hits[i];
		var renderer = hit.collider.renderer;
		if (renderer) {
			renderer.material.shader = Shader.Find("Transparent/Diffuse");
			renderer.material.color.a = 0.3;
		}
	}
}

Note: This function will return false if you cast a ray from inside a sphere to the outside; this in an intended behaviour.

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

最后修改:2011年4月9日 Saturday 11:30

本脚本参考基于Unity 3.4.1f5

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