Rigidbody.SweepTest 扫描测试

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

Parameters参数

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.

如果一个刚体碰到任何东西触发测试,如果它是穿过场景。

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.Raycast,并返回最接近的所有碰撞(如果有)报告。这对于AI代码非常有用,当你需要知道如果一个物体想要适配不与任何东西碰撞的地方。

参见: 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
	//投下一个刚体向前10米,看它是否将要撞上任何东西
	
	if (rigidbody.SweepTest (transform.forward, hit, 10)) {
		Debug.Log(hit.distance + "mts distance to obstacle");
	}
}
最后修改:2011年2月10日 Thursday 21:19

本脚本参考基于Unity 3.4.1f5

英文部分版权属©Unity公司所有,中文部分© Unity圣典 版权所有,未经许可,严禁转载 。