返回介绍

Physics.CapsuleCast 胶囊投射

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

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

C# => public static bool CapsuleCast(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 返回值

bool True when the capsule sweep intersects any collider, otherwise false.

胶囊扫描到任意交叉碰撞器返回true,否则返回false。

Description 描述

Casts a capsule against all colliders in the scene and returns detailed information on what 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 for the first collider 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.

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

Notes: CapsuleCast will not detect colliders for which the capsule overlaps 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 CapsuleCast will hit the collider at it's new position.

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

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

JavaScript:

	function Update () {
		var hit : RaycastHit;
		var charContr : CharacterController = GetComponent.<CharacterController>();
		var p1 : Vector3 = transform.position + charContr.center + 
					Vector3.up * (-charContr.height*0.5);
		var p2 : Vector3 = p1 + Vector3.up * charContr.height;
		// Cast character controller shape 10 meters forward to see if it is about to hit anything
		if (Physics.CapsuleCast (p1, p2, charContr.radius, transform.forward, hit, 10)) {
			distanceToObstacle = hit.distance;
		}
	}

C#:

using UnityEngine;
using System.Collections;
 
public class ExampleClass : MonoBehaviour {
    void Update() {
        RaycastHit hit;
        CharacterController charContr = GetComponent<CharacterController>();
        Vector3 p1 = transform.position + charContr.center + Vector3.up * -charContr.height * 0.5F;
        Vector3 p2 = p1 + Vector3.up * charContr.height;
        float distanceToObstacle = 0;
 
        // Cast character controller shape 10 meters forward to see if it is about to hit anything.
        if (Physics.CapsuleCast(p1, p2, charContr.radius, transform.forward, out hit, 10))
            distanceToObstacle = hit.distance;
    }
}

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

C# => public static bool CapsuleCast(Vector3 point1, Vector3 point2, float radius, Vector3 direction, out RaycastHit hitInfo, 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.
扫描的最大长度。
hitInfoIf true is returned, hitInfo will contain more information about where the collider was hit (See Also: RaycastHit).
hitInfo包含更多的碰到碰撞器的信息。
layerMaskA Layer mask that is used to selectively ignore colliders when casting a capsule.
选择投射的层蒙版。
queryTriggerInteractionSpecifies whether this query should hit Triggers.
指定是否查询碰到触发器。

Description 描述

Physics

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

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

发布评论

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