foreword
Need to use Physics.OverlapBox
to detect surrounding objects. Because the distance is required to be more precise, it is used Gizmos.DrawWireCube
to draw the range, and Physics.OverlapBox
the detected detection range is found to be inconsistent with the drawn graphic range.
principle
OverlapBox
It is a method from the Nvidia PhysX system by consulting the information . Its actual size is twice the set size. (compare colliders or DrawWireCube
).
solution
It is very simple to know the principle, just draw the size*2 directly, the code is as follows:
public Vector3 size = new Vector3(2, 2.1f, 5.7f);
private void FixedUpdate()
{
Collider[] cols = Physics.OverlapBox(this.transform.position, size,LayerMask.NameToLayer("layername"));
Debug.Log(cols.Length);
}
#if UNITY_EDITOR
private void OnDrawGizmos()
{
Gizmos.DrawWireCube(this.transform.position, size * 2);
}
#endif
After testing Physics.OverlapSphere
, it can be used directly Gizmos.DrawWireSphere
, no need to put the size * 2.