返回介绍

Physics.CapsuleCastAll 胶囊投射全部

发布于 2019-12-18 15:38:15 字数 6942 浏览 1117 评论 0 收藏 0

JavaScript => public static function CapsuleCastAll(point1: Vector3, point2: Vector3, radius: float, direction: Vector3, maxDistance: float = Mathf.Infinity, layerMask: int = DefaultRaycastLayers, queryTriggerInteraction: QueryTriggerInteraction = QueryTriggerInteraction.UseGlobal): RaycastHit[] ;

C# => public static RaycastHit[] CapsuleCastAll(Vector3 point1, Vector3 point2, float radius, Vector3 direction, float maxDistance = Mathf.Infinity, int layerMask = DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal);

Parameters 参数

point1The center of the sphere at the start of the capsule.
球体开始中心点。
point2The center of the sphere at the end of the capsule.
球体结束的中心点。
radiusThe radius of the capsule.
胶囊的半径。
directionThe direction into which to sweep the capsule.
胶囊扫描的方向。
maxDistanceThe max length of the sweep.
扫描的最大长度。
layerMaskA Layer mask that is used to selectively ignore colliders when casting a capsule.
选择投射的层蒙版。
queryTriggerInteractionSpecifies whether this query should hit Triggers.
指定是否查询碰到触发器。

Returns 返回值

RaycastHit[] An array of all colliders hit in the sweep.

返回扫描到的所有碰撞器RaycastHit信息。

Description 描述

Like Physics.CapsuleCast, but this function will return all hits the capsule sweep intersects.

Physics.CapsuleCast,但这个函数将返回所有胶囊扫描交叉碰到的对象。

Casts a capsule against all colliders in the scene and returns detailed information on each collider which was hit. The capsule is defined by the two spheres with radius around point1 and point2, which form the two ends of the capsule. Hits are returned all colliders which would collide against this capsule if the capsule was moved along direction. This is useful when a Raycast does not give enough precision, because you want to find out if an object of a specific size, such as a character, will be able to move somewhere without colliding with anything on the way.

投射一个胶囊针对场景中所有碰撞器,并返回所有碰到的每个碰撞器的细节信息。 胶囊是由point1和point2位置的两个球带有radius半径形成胶囊的两端定义。返回胶囊沿direction方向碰到的第一个碰撞器。这通常用于投射不需足够的精度,因为你要找出一个特定大小的物体,如人物,能移动到某处而不在途中碰撞到任何东西。

Notes: For colliders that overlap the capsule at the start of the sweep, the output direction is set opposite to the direction of the sweep, distance is set to zero as well as zero vector gets returned in the position field of the RaycastHit structure. You might want to check whether this is the case in your particular query and perform additional queries to refine the result. 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 capsule cast will hit the collider at it's new position.

如果从脚本或动画移动碰撞器,需要有至少一个在FixedUpdate中执行,使得物理库可以更新它的数据结构,在CapsuleCast碰到碰撞器之前是在它的新位置。

See Also: Physics.SphereCast, Physics.CapsuleCast, Physics.Raycast, Rigidbody.SweepTest.

JavaScript:

#pragma strict
function Update() {
	var hits: RaycastHit[];
	var charCtrl: CharacterController = GetComponent.<CharacterController>();
	var p1: Vector3 = transform.position + charCtrl.center + Vector3.up * -charCtrl.height * 0.5F;
	var p2: Vector3 = p1 + Vector3.up * charCtrl.height;
	// Cast character controller shape 10 meters forward, to see if it is about to hit anything
	hits = Physics.CapsuleCastAll(p1, p2, charCtrl.radius, transform.forward, 10);
	// to use a transparent Shader
	for (var i: int = 0; i < hits.Length; i++) {
		var hit: RaycastHit = hits[i];
		var rend: Renderer = hit.transform.GetComponent.<Renderer>();
		if (rend) {
			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;
        CharacterController charCtrl = GetComponent<CharacterController>();
        Vector3 p1 = transform.position + charCtrl.center + Vector3.up * -charCtrl.height * 0.5F;
        Vector3 p2 = p1 + Vector3.up * charCtrl.height;
 
        // Cast character controller shape 10 meters forward, to see if it is about to hit anything
        hits = Physics.CapsuleCastAll(p1, p2, charCtrl.radius, transform.forward, 10);
 
		// Change the material of all hit colliders
		// to use a transparent Shader
        for (int i = 0; i < hits.Length; i++) {
            RaycastHit hit = hits[i];
            Renderer rend = hit.transform.GetComponent<Renderer>();
 
            if (rend) {
                rend.material.shader = Shader.Find("Transparent/Diffuse");
                Color tempColor = rend.material.color;
                tempColor.a = 0.3F;
                rend.material.color = tempColor;
            }
        }
    }
}

Physics

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

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

发布评论

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