赞
踩
static function Raycast (origin : Vector3, direction : Vector3, distance : float = Mathf.Infinity, layerMask : int = kDefaultRaycastLayers) : bool
返回值
当光线投射与任何碰撞器交叉时为真,否则为假。
描述
在场景中投下可与所有碰撞器碰撞的一条光线。
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 : Vector3, direction : Vector3, out hitInfo : RaycastHit, distance : float = Mathf.Infinity, layerMask : int = kDefaultRaycastLayers) : bool
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 : Ray, distance : float = Mathf.Infinity, layerMask : int = kDefaultRaycastLayers) : bool
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); if (Physics.Raycast (ray, 100)) { print ("Hit something"); }
• static function Raycast (ray : Ray, out hitInfo : RaycastHit, distance : float = Mathf.Infinity, layerMask : int = kDefaultRaycastLayers) : bool
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); }
使用光线投射碰撞可以达到的效果:在还没有发生真正的物理碰撞之前,就响应碰撞。
不用使用物理碰撞接口 OnControllerColliderHit
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。