OverlapCircleAll 没有拾取其他物体

Get*_*awn 4 c# overlap unity-game-engine

我正在尝试获取当前对象距离内的所有对象。将maxShootDistance被设置为3,并且当一个对象,它是部分ShootAt层获得接近/在圈内,它永远不会拾取,以及我的调试输出0。为什么它不捡起另一个物体?

public class QuckShot : Gun {

    void Start () {
        StartCoroutine(shoot());
    }

    IEnumerator shoot(){
        while(true){
            Collider2D[] hitColliders = Physics2D.OverlapCircleAll(transform.position, maxShootDistance, LayerMask.NameToLayer("ShootAt"));
            Debug.Log(hitColliders.Length); // This is always returning zero
            /*
             * Snipped other bits of code
             */
            yield return new WaitForSeconds(shootSpeed);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

以下是分配给应该被拾取的对象的属性:

女性属性

为什么我的代码没有拾取对象?

rut*_*ter 5

这个电话:

LayerMask.NameToLayer("ShootAt")
Run Code Online (Sandbox Code Playgroud)

NameToLayer返回图层索引(即:7),但OverlapCircleAll需要按位图层掩码(即:第 7 位启用)。

在某些情况下,如果图层索引碰巧与所需的图层蒙版有一点或两个共同点,则此类事情可能会起作用。无论哪种方式,它最多会得到不直观和混合的结果。

您可以使用按位左移运算符构造图层蒙版:

1 << LayerMask.NameToLayer("ShootAt");
Run Code Online (Sandbox Code Playgroud)

(您可以使用按位 OR 添加其他层。)