ray | 光线的起点和方向。 |
maxDistance | 从射线起点开始,允许射线命中的最大距离。 |
layerMask | 层遮罩,用于在投射射线时有选择地忽略碰撞体。 |
queryTriggerInteraction | 指定该查询是否应该命中触发器。 |
RaycastHit[] RaycastHit 对象的数组。注意,这些结果的顺序未定义。
向场景中投射射线并返回所有命中对象。注意,这些结果的顺序未定义。
另请参阅: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 不会检测到碰撞体。
origin | 射线在世界坐标系中的起点。 |
direction | 射线的方向。 |
maxDistance | 从射线起点开始,允许射线命中的最大距离。 |
layermask | 层遮罩,用于在投射射线时有选择地忽略碰撞体。 |
queryTriggerInteraction | 指定该查询是否应该命中触发器。 |
另请参阅:Raycast。
见上面的示例。
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.