OverlapBox比我问他的还要多

时间:2019-12-28 16:13:32

标签: visual-studio unity3d unityscript

我有一个人物,在其面部上创建了一个重叠框,我通过小控件绘制了它,但是由于某种原因,它甚至在对象进入小控件绘制的区域之前就检测到了对象。如果我创建了overlaySphere,那么一切都会很好

   private Vector3 overlapPos, overlapSize;
   private void Attack()
    {
        if(Input.GetKeyDown(KeyCode.Mouse1))
        {
            var cam = Vector3.Scale(transform.forward, new Vector3(1, 0, 1)).normalized;
            overlapPos = new Vector3(transform.position.x, transform.position.y + 1.2f, transform.position.z);
            overlapPos += cam*1.2f ;
            overlapSize = new Vector3(2.5f, 1.7f, 2f);
            Physics.SyncTransforms();
            var enemys = Physics.OverlapBox(overlapPos, overlapSize, transform.localRotation);
            foreach (Collider col in enemys)
                print(col.gameObject.name);
            //enemyGO.GetComponent<HealthComponent>().TakeDamage(10);
        }
    }

    void FixedUpdate()
    {
        Attack();
    }

    private void OnDrawGizmos()
    {
        Gizmos.color = Color.green;
        Gizmos.matrix = Matrix4x4.TRS(overlapPos,transform.localRotation, overlapSize);
        Gizmos.DrawWireCube(Vector3.zero, Vector3.one);
    }

1 个答案:

答案 0 :(得分:0)

请注意,OverlapBox的第二个参数称为 halfExtends

  

每个尺寸的框尺寸的一半

因此,您宁愿使用重叠框的一半大小

Physics.OverlapBox(overlapPos, overlapSize * 0.5f, transform.localRotation);

或者必须更改Gizmos才能显示校正后的双倍大小

Gizmos.DrawWireCube(Vector3.zero, Vector3.one * 2);