Unity--光线投射碰撞Physics.Raycast和Physics.RaycastAll
2015-03-29 00:08
489 查看
Physics.Raycast
光线投射
static function Raycast (origin : Vector3, direction : Vector3, distance :float = Mathf.Infinity, layerMask :
int = kDefaultRaycastLayers) : bool
Parameters参数
originThe 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 : Vector3, direction : Vector3, out
hitInfo : RaycastHit, distance :
float = Mathf.Infinity, layerMask :
int = kDefaultRaycastLayers) : bool
Parameters参数
originThe 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 : Ray, distance :
float = Mathf.Infinity, layerMask :
int = kDefaultRaycastLayers) : bool
Parameters参数
rayThe 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 : Ray, out
hitInfo : RaycastHit, distance :
float = Mathf.Infinity, layerMask :
int = kDefaultRaycastLayers) : bool
Parameters参数
rayThe 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
相关文章推荐
- Unity 使用物理射线(Physics.Raycast),实现扇形区域碰撞检测三种方法
- Unity Raycast 光线投射
- Unity 使用物理射线(Physics.Raycast),实现扇形区域碰撞检测三种方法
- Raycast光线投射,Linecast,RaycastAll
- Unity Physics.Raycast 之 layerMask
- 游戏架构其九:光线投射和天空 { Raycast and Sky }
- Physics.Raycast指定碰撞层
- unity中htc vive物理射线检测碰撞的方法—Phsics.Raycast()
- Unity Physics.Raycast的参数layermask不起作用
- unity中htc vive射线与UI碰撞检测-graphcs.Raycast()
- Unity Physics.Raycast 深入理解
- Unity-Physics.Raycast
- Physics.raycast
- 光线与盒体相交检测(Ray-OBB)可用于VR中Cursor与控件的碰撞检测
- [6.837]A1:光线投射法(Ray-Casting)
- 【unity5学习记录】 raycast光线投射碰撞 17.8.5
- 使用Unity3d的Physics.Raycast()的用法做子弹射击
- 对Physics.Raycast和Ray(射线)的理解 和 分层layer的使用
- unity3d问题集 <3> 对Physics.Raycast和Ray(射线)的理解
- Unity中射线Ray和RaycastHit的简单介绍