Legacy Documentation: Version 5.2
LanguageEnglish
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

Rigidbody.SweepTest

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Sumbission failed

For some reason your suggested change could not be submitted. Please try again in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Switch to Manual
public function SweepTest(direction: Vector3, out hitInfo: RaycastHit, maxDistance: float = Mathf.Infinity, queryTriggerInteraction: QueryTriggerInteraction = QueryTriggerInteraction.UseGlobal): bool;
public bool SweepTest(Vector3 direction, out RaycastHit hitInfo, float maxDistance = Mathf.Infinity, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal);
public function SweepTest(direction: Vector3, out hitInfo: RaycastHit, maxDistance: float = Mathf.Infinity, queryTriggerInteraction: QueryTriggerInteraction = QueryTriggerInteraction.UseGlobal): bool;
public bool SweepTest(Vector3 direction, out RaycastHit hitInfo, float maxDistance = Mathf.Infinity, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal);

Parameters

direction The direction into which to sweep the rigidbody.
hitInfo If true is returned, hitInfo will contain more information about where the collider was hit (See Also: RaycastHit).
distance The length of the sweep.

Returns

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

Description

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.

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

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

var collisionCheckDistance: float;
var aboutToCollide: boolean;
var distanceToCollision: float;
var rb: Rigidbody;

function Start() { rb = GetComponent.<Rigidbody>(); }

function Update () { // Cast the rigidbody's shape forward to see if it is about to hit anything. var hit : RaycastHit;

if (rb.SweepTest(transform.forward, hit, collisionCheckDistance)) { aboutToCollide = true; distanceToCollision = hit.distance; } }
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; } } }
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961