Unity3d Physics 射线粗谈

射线有很多Raycast就是一根直线出去,而其他的有形状的如SphereCast,BoxCast等就是相当于射个形状出去,然后进行检测,这里重点说SphereCast,其他的几种类似。


Physics.SphereCast

Physics.SphereCast 球形射线。相比Physics.Raycast,就是把射线的宽度给增加了。可以想象为把球向某个方向移动,在移动过程中去检测。这里有个坑就是:它不能检测到起点半径之内的物体,也就是说发出射线的时候就已经包含在球半径内的话是不能被检测到的,如果要检测半径内的使用Physics.OverlapSphere来进行检测。

下面是参数,API的中文翻译很坑,为加强理解加了如下注释 
  • origin

The center of the sphere at the start of the sweep.
在扫描起点的球体中心点。

  • radius

The radius of the sphere.
球体的半径。

  • direction

The direction into which to sweep the sphere.
球体扫描的方向。其实就是射线发射方向

  • hitInfo

If true is returned, hitInfo will contain moreinformation about where the collider was hit (See Also:RaycastHit).
如果返回true,hitInfo将包含碰到器碰撞的更多信息。

  • distance

The length of the sweep
扫描的长度。即射线的长度,球向前移动的最大距离

  • layerMask

A Layer mask that is used to selectively ignorecolliders when casting a capsule.
根据Layer mask层的不同来忽略碰撞体。 即只检查该层上的碰撞体。想要在检测时排除某一个层,使用~运算符,如排除Ball这个层:~LayerMask.GetMask("Ball"),要知道这个的原理移步本人的C#枚举多选的原理与实现文章。



猜你喜欢

转载自blog.csdn.net/qwsx789/article/details/51028249