How does unityOverlapBox draw the range correctly

foreword

Need to use Physics.OverlapBoxto detect surrounding objects. Because the distance is required to be more precise, it is used Gizmos.DrawWireCubeto draw the range, and Physics.OverlapBoxthe detected detection range is found to be inconsistent with the drawn graphic range.

principle

OverlapBoxIt 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.

Effect

insert image description here

Guess you like

Origin blog.csdn.net/qq_39162826/article/details/120198438