返回介绍

Physics.RaycastAll 射线投射列表

发布于 2019-12-18 15:38:16 字数 5934 浏览 1632 评论 0 收藏 0

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

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

Parameters 参数

rayThe starting point and direction of the ray.
带有开始点和方向的射线。
maxDistanceThe max distance the rayhit is allowed to be from the start of the ray.
从射线开始允许投射的最大距离。
layerMaskA Layer mask that is used to selectively ignore colliders when casting a ray.
选择投射的层蒙版。
queryTriggerInteractionSpecifies whether this query should hit Triggers.
指定是否查询碰到触发器。

Description 描述

Casts a ray through the scene and returns all hits. Note that order is not guaranteed.

在场景投射一条射线并返回所有碰撞。注意不保证顺序。

JavaScript:

#pragma strict
function Update() {
	var hits: RaycastHit[];
	hits = Physics.RaycastAll(transform.position, transform.forward, 100.0F);
	for (var i: int = 0; i < hits.Length; i++) {
		var hit: RaycastHit = hits[i];
		var rend: Renderer = hit.transform.GetComponent.<Renderer>();
		if (rend) {
			// to use a transparent shader.
			rend.material.shader = Shader.Find("Transparent/Diffuse");
			var tempColor: Color = rend.material.color;
			tempColor.a = 0.3F;
			rend.material.color = tempColor;
		}
	}
}

C#:

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;
            }
        }
    }
}

Notes: Raycasts will not detect colliders for which the raycast origin is inside the collider. If you move colliders from scripting or by animation, there needs to be at least one FixedUpdate executed so that the physics library can update it's data structures, before a Raycast will hit the collider at it's new position.

注意,射线投射不会从碰撞器内部检测碰撞。如果你用脚本或者动画移动碰撞器,至少需要一个在FixedUpdate来执行物理库更新数据结构,Raycast在它的新位置之前碰到碰撞器。


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

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

Parameters 参数

originThe starting point of the ray in world coordinates.
射线的开始点。
directionThe direction of the ray.
射线的方向。
maxDistanceThe max distance the rayhit is allowed to be from the start of the ray.
从射线开始允许投射的最大距离。
layermaskA Layer mask that is used to selectively ignore colliders when casting a ray.
选择投射的层蒙版。
queryTriggerInteractionSpecifies whether this query should hit Triggers.
指定是否查询碰到触发器。

Description 描述

See Also: Raycast.

Physics

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文