Physics.CapsuleCast 胶囊投射

static function CapsuleCast (point1 : Vector3, point2 : Vector3, radius : float, direction : Vector3, distance : float = Mathf.Infinity, layerMask : int = kDefaultRaycastLayers) : bool
static function CapsuleCast (point1 : Vector3, point2 : Vector3, radius : float, direction : Vector3, out hitInfo : RaycastHit, distance : float = Mathf.Infinity, layerMask : int = kDefaultRaycastLayers) : bool

Parameters参数

Returns

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

当胶囊投射时与碰撞体交叉时,返回真,否则返回假。

Description描述

Casts a capsule against all colliders in the scene and returns detailed information on what was hit.

对场景中的碰撞体进行胶囊的投射,如果相交,返回碰撞体的raycasthit信息。

The capsule is defined by the two spheres with radius around point1 and point2, which form the two ends of the capsule. Hits are returned for the first collider which would collide against this capsule if the capsule was moved along direction. This is useful when a Raycast does not give enough precision, because you want to find out if an object of a specific size, such as a character, will be able to move somewhere without colliding with anything on the way.

胶囊的描述:point1 ,point2为上下两个结束点,radius为半径。对胶囊做投射时,如果和碰撞体碰撞,raycasthit 结构返回碰撞信息。这可以用在光线投射无法满足要求时的情况下。例如要判断角色(有空间大小尺寸)是否和一个物体发生碰撞。

参见:Physics.SphereCast, Physics.CapsuleCastAll, Physics.Raycast, Rigidbody.SweepTest

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
	void Update() {
		RaycastHit hit;
		CharacterController charContr = GetComponent<CharacterController>();
		Vector3 p1 = transform.position + charContr.center + Vector3.up * -charContr.height * 0.5F;
		Vector3 p2 = p1 + Vector3.up * charContr.height;
		if (Physics.CapsuleCast(p1, p2, charContr.radius, transform.forward, out hit, 10))
			distanceToObstacle = hit.distance;

	}
}
function Update () {
	var hit : RaycastHit;
	var charContr : CharacterController = GetComponent(CharacterController);
	var p1 : Vector3 = transform.position + charContr.center +
	Vector3.up * (-charContr.height*0.5);
	var p2 : Vector3 = p1 + Vector3.up * charContr.height;
	// Cast character controller shape 10 meters forward, to see if it is about to hit anything
	//向前投射角色物体10米,是否与其他物体碰撞
	if (Physics.CapsuleCast (p1, p2, charContr.radius, transform.forward, hit, 10)) {
		distanceToObstacle = hit.distance;
	}
}
最后修改:2011年4月9日 Saturday 12:10

本脚本参考基于Unity 3.4.1f5

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