返回介绍

Rigidbody.SweepTest 扫描测试

发布于 2019-12-18 15:38:29 字数 4211 浏览 1013 评论 0 收藏 0

JavaScript => public function SweepTest(direction: Vector3, out hitInfo: RaycastHit, maxDistance: float = Mathf.Infinity, queryTriggerInteraction: QueryTriggerInteraction = QueryTriggerInteraction.UseGlobal): bool;
C# => public bool SweepTest(Vector3 direction, out RaycastHit hitInfo, float maxDistance = Mathf.Infinity, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal);

Parameters 参数

directionThe direction into which to sweep the rigidbody.
扫描该刚体的方向。
hitInfoIf true is returned, hitInfo will contain more information about where the collider was hit (See Also: RaycastHit).
hitInfo包含更多碰到的信息。
maxDistanceThe length of the sweep.
扫描的长度。
queryTriggerInteractionSpecifies whether this query should hit Triggers.
指定是否查询碰到触发器。

Returns 返回值

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

返回bool类型,当为true时,刚体扫描相交的任意碰撞器,否则为false。

Description 描述

Tests if a rigidbody would collide with anything, if it was moved through the scene.

如果一个刚体碰到任何东西触发测试。

Tests if a rigidbody would collide with anything, if it was moved through the scene. This is similar to doing a Physics.Raycast for all points contained in any of a Rigidbody's colliders and returning the closest of all hits (if any) reported. This is useful for AI code, say if you need to know that an object would fit through a gap without colliding with anything.

这类似于为任何一个刚体的碰撞器包含的所有点做一个Physics.Raycast,并返回最接近的所有碰撞(如果有)报告。这对于AI代码非常有用,当你需要知道如果一个物体想要适配不与任何东西碰撞的地方。

Note that this function only works when a primitive collider type (sphere, cube or capsule) or a convex mesh is attached to the rigidbody object - concave mesh colliders will not work, although they can be detected in the scene by the sweep.

注意该函数仅工作与原始碰撞器类型(球体、立方体或胶囊)或凸网格碰撞器,凹网格碰撞器不工作,尽管它在场景能扫描检测。

See Also: Physics.SphereCast, Physics.CapsuleCast, Rigidbody.SweepTestAll.

JavaScript:

#pragma strict
public var collisionCheckDistance: float;
public var aboutToCollide: boolean;
public var distanceToCollision: float;
public var rb: Rigidbody;
function Start() {
	rb = GetComponent.<Rigidbody>();
}
function Update() {
	var hit: RaycastHit;
	if (rb.SweepTest(transform.forward, hit, collisionCheckDistance)) {
		aboutToCollide = true;
		distanceToCollision = hit.distance;
	}
}

C#:

using UnityEngine;
using System.Collections;
 
public class ExampleClass : MonoBehaviour {
    public float collisionCheckDistance;
    public bool aboutToCollide;
    public float distanceToCollision;
    public Rigidbody rb;
 
    void Start() {
        rb = GetComponent<Rigidbody>();
    }
 
    void Update() {
        RaycastHit hit;
        if (rb.SweepTest (transform.forward, out hit, collisionCheckDistance)) {
            aboutToCollide = true;
            distanceToCollision = hit.distance;
        }
    }
}

Rigidbody

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

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

发布评论

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