Rigidbody.SweepTest 扫描测试

function SweepTest (direction : Vector3, out hitInfo : RaycastHit, distance : float = Mathf.Infinity) : bool



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



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, when you need to know if an object would fit somewhere without colliding with anything.


参见: Physics.SphereCast, Physics.CapsuleCast, Rigidbody.SweepTestAll

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
	public RaycastHit hit;
	void Update() {
		if (rigidbody.SweepTest(transform.forward, ref hit, 10))
			Debug.Log(hit.distance + "mts distance to obstacle");

var hit : RaycastHit;

function Update () {
	// Cast rigidbody shape 10 meters forward, to see if it is about to hit anything
	if (rigidbody.SweepTest (transform.forward, hit, 10)) {
		Debug.Log(hit.distance + "mts distance to obstacle");
Page last updated: 2011-2-10