public static RaycastHit[] RaycastAll (Ray ray, float maxDistance= Mathf.Infinity, int layerMask= DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction= QueryTriggerInteraction.UseGlobal);

参数

ray光线的起点和方向。
maxDistance从射线起点开始,允许射线命中的最大距离。
layerMask 层遮罩,用于在投射射线时有选择地忽略碰撞体。
queryTriggerInteraction指定该查询是否应该命中触发器。

描述

向场景中投射射线并返回所有命中对象。注意,不保证顺序。

另请参阅:Raycast

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour { void Update() { RaycastHit[] hits; hits = Physics.RaycastAll(transform.position, transform.forward, 100.0F);

for (int i = 0; i < hits.Length; i++) { RaycastHit hit = hits[i]; Renderer rend = hit.transform.GetComponent<Renderer>();

if (rend) { // Change the material of all hit colliders // to use a transparent shader. rend.material.shader = Shader.Find("Transparent/Diffuse"); Color tempColor = rend.material.color; tempColor.a = 0.3F; rend.material.color = tempColor; } } } }

注意:对于射线投射起点位于碰撞体内的情况,Raycast 不会检测到碰撞体。


public static RaycastHit[] RaycastAll (Vector3 origin, Vector3 direction, float maxDistance= Mathf.Infinity, int layerMask= DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction= QueryTriggerInteraction.UseGlobal);

参数

origin射线在世界坐标系中的起点。
direction射线的方向。
maxDistance从射线起点开始,允许射线命中的最大距离。
layermask 层遮罩,用于在投射射线时有选择地忽略碰撞体。
queryTriggerInteraction指定该查询是否应该命中触发器。

描述

另请参阅:Raycast

见上面的示例。