public static bool CheckBox(Vector3 center, Vector3 halfExtents, Quaternion orientation = Quaternion.identity, int layermask = DefaultRaycastLayers, QueryTriggerInteractionqueryTriggerInteraction = QueryTriggerInteraction.UseGlobal);
今天用這個玩意,自己把自己坑了,主要還是因爲不熟,記錄一下,增強印象!
我使用BoxCollider記錄Bounding範圍的。
第一個參數,要用BoxCollider所依附的Transform.position+BoxCollider.center獲得BoundingBox在世界座標系內的位置。
第二個參數,需要去BoxCollider的Size的一半,也就是 BoxCollider.size/2
第三個參數,我一開始沒搞明白他的意思,填的默認值,後來發現碰撞檢測老是不正確,琢磨了半天,如果你只設置一個Box的中點和範圍,那麼它永遠是平行於x,y軸的。
so,這個參數的意義是,box的旋轉方向,使之能夠跟着物體一起旋轉,我試着將BoxCollider所依附的Transform.rotation.獲得了正確的結果。
第四個參數就很容易理解了,用來控制檢測那些層的物件
最後一個參數是設置是否需要忽略Trigger。
//Trace Box toward obstacles
string[] masked_obstacles = { "Default", "Buildings" };
BoxCollider[] bounds = building_object.BoundingBoxes;
if( bounds.Length > 0 )
{
Vector3 world_pos = bounds[0].transform.position;
Quaternion rotation = bounds[0].transform.rotation;
for (int i = 0; i < bounds.Length; i++)
{
if (Physics.CheckBox(world_pos + bounds[i].center, bounds[i].size / 2, rotation, LayerMask.GetMask(masked_obstacles), QueryTriggerInteraction.Ignore))
{
return EPlaceableObjectState.Conflict;
}
}
}
如上面代碼,我用這種用法來檢測,自由建築系統放置建築時,建築是否會和其他物件衝突。