Unity--光线投射碰撞Physics.Raycast和Physics.RaycastAll

Physics.Raycast 光线投射

static function Raycast (origin : Vector3direction : Vector3distance : float = Mathf.InfinitylayerMask : int = kDefaultRaycastLayers) : bool

Parameters参数

  • origin
    The starting point of the ray in world coordinates.
    在世界坐标,射线的起始点。
  • direction
    The direction of the ray.
    射线的方向。
  • distance
    The length of the ray
    射线的长度。
  • layerMask
    A Layer mask that is used to selectively ignore colliders when casting a ray. 
    只选定Layermask层内的碰撞器,其它层内碰撞器忽略。

返回值

当光线投射与任何碰撞器交叉时为真,否则为假。

描述

在场景中投下可与所有碰撞器碰撞的一条光线。

function Update () {
	var fwd = transform.TransformDirection (Vector3.forward);
	if (Physics.Raycast (transform.position, fwd, 10)) {
		print ("There is something in front of the object!");
	}
}

注意:如果从一个球型体的内部到外部用光线投射,返回为假。 

tatic function Raycast (origin : Vector3direction : Vector3out hitInfo : RaycastHitdistance : float = Mathf.InfinitylayerMask : int = kDefaultRaycastLayers) : bool

Parameters参数

  • origin
    The starting point of the ray in world coordinates.
    在世界坐标,射线的起始点。
  • direction
    The direction of the ray.
    射线的方向。
  • distance
    The length of the ray
    射线的长度。
  • hitInfo
    If true is returned, hitInfo will contain more information about where the collider was hit (See Also:  RaycastHit ).
    如果返回true,hitInfo将包含碰到器碰撞的更多信息。
  • layerMask
    A Layer mask that is used to selectively ignore colliders when casting a ray. 
    只选定Layermask层内的碰撞器,其它层内碰撞器忽略。

Returns

bool – True when the ray intersects any collider, otherwise false.

当光线投射与任何碰撞器交叉时为真,否则为假。

Description描述

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

在场景中投下可与所有碰撞器碰撞的一条光线,并返回碰撞的细节信息。

function Update () {
	var hit : RaycastHit;
	if (Physics.Raycast (transform.position, -Vector3.up, hit)) {
		var distanceToGround = hit.distance;
	}
}
又一个例子:

C#JavaScript
// Raycast up to 100 meters forward
//光线向前投射100米远
function Update () {
	var hit : RaycastHit;
	if (Physics.Raycast (transform.position, -Vector3.up, hit, 100.0)) {
		var distanceToGround = hit.distance;
	}
}

static function Raycast (ray : Raydistance : float = Mathf.InfinitylayerMask : int = kDefaultRaycastLayers) : bool

Parameters参数

  • ray
    The starting point and direction of the ray.
    射线的起点和方向
  • distance
    The length of the ray
    射线的长度。
  • layerMask
    A Layer mask that is used to selectively ignore colliders when casting a ray. 
    只选定Layermask层内的碰撞器,其它层内碰撞器忽略。

Returns

bool – True when the ray intersects any collider, otherwise false.

当光线投射与任何碰撞器交叉时为真,否则为假。

Description描述

Same as above using ray.origin and ray.direction instead of origin and direction.

使用ray.origin和ray.direction同上,替代origin和direction。

  • C#
  • JavaScript
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, 100)) {
	print ("Hit something");
}

• static function Raycast (ray : Rayout hitInfo : RaycastHitdistance : float = Mathf.InfinitylayerMask : int = kDefaultRaycastLayers) : bool

Parameters参数

  • ray
    The starting point and direction of the ray.
    射线的起点和方向
  • distance
    The length of the ray
    射线的长度
  • hitInfo
    If true is returned, hitInfo will contain more information about where the collider was hit (See Also:  RaycastHit ).
    如果返回true,hitInfo将包含碰到器碰撞的更多信息。
  • layerMask
    A Layer mask that is used to selectively ignore colliders when casting a ray. 
    只选定Layermask层内的碰撞器,其它层内碰撞器忽略。

Returns

bool – True when the ray intersects any collider, otherwise false.

当光线投射与任何碰撞器交叉时为真,否则为假。

Description描述

Same as above using ray.origin and ray.direction instead of origin and direction.

使用ray.origin和ray.direction同上,替代origin和direction。

var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast (ray, hit, 100)) {
	Debug.DrawLine (ray.origin, hit.point);
}

方法二:Physics.RaycastAll 光线投射

static function  RaycastAll  ( ray  :  Ray distance  : float =  Mathf.Infinity , layerMask : int = kDefaultRaycastLayers) :  RaycastHit []
static function  RaycastAll  ( origin  :  Vector3 , direction :  Vector3 , distance : float =  Mathf.Infinity , layermask : int = kDefaultRaycastLayers) :  RaycastHit []

投射一条光线并返回所有碰撞,也就是投射光线并返回一个RaycastHit[]结构体。

使用光线投射碰撞可以达到的效果:在还没有发生真正的物理碰撞之前,就响应碰撞。

不用使用物理碰撞接口 OnControllerColliderHit


你可能感兴趣的:(unity3d)