direction | 扫描刚体的方向。 |
hitInfo | 如果返回 true,则 hitInfo 将包含有关碰撞体的撞击位置的更多信息(另请参阅:RaycastHit)。 |
maxDistance | 扫描的长度。 |
queryTriggerInteraction | 指定该查询是否应该命中触发器。 |
bool 当刚体扫描与任何碰撞体交叠时为 true,否则为 false。
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.
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.
另请参阅:Physics.SphereCast、Physics.CapsuleCast、Rigidbody.SweepTestAll。
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; } } }