SphereCast not detecting objects on layer

Hey guys,
i have been banging my head here for a while here now with a (probably) simple problem.
i wanted to detect my players using spherecasts and send those to my client but for some reason my assigned objects arent detected.
i know for sure they are properly assigned to that layer, any ideas whats wrong here?

// Player initiation
    public void Initialize(int _id, string _username)
    {
        id = _id;
        username = _username;
        this.gameObject.layer = LayerMask.NameToLayer("Players");

        Debug.Log($"Set player to layer #{this.gameObject.layer}");

        Debug.Log($"Objects on player layer:{string.Join(",", Resources.FindObjectsOfTypeAll(typeof(GameObject)).Where(i => (i as GameObject).layer == 8).Select(i => (i as GameObject).name))}");
        inputs = new bool[6];
    }
// SphereCast detection.
 private void SendPlayersAroundPlayer()
    {
        foreach (RaycastHit hit in Physics.SphereCastAll(this.transform.position, 50f, Vector3.forward, 50f, LayerMask.NameToLayer("Players")))
        {
            Debug.Log(hit.transform.name); // Never gets printed cause SphereCastAll returns 0 hits
            //ServerSend.SendPlayer(id, hit.transform.GetComponent<Player>());
        }
        ServerSend.SendPlayer(this.id, this);
    }

IIRC spherecast does not detect collision if the start of the sphere already overlaps with the other object.

This can be inconvenient, but that’s how it is.

6590323--748810--upload_2020-12-4_23-48-48.png

4 Likes

You can mitigate it with a extra overlap call if its important.

i noticed this during testing, but still my 2 player objects are in an open area so no colissions should interfere.

6591781--749095--unitled.png

Hmm, i tested this now also for a while yielding the same result.
i’m able to detect my gameobjects untill i filter on layers, at that point i keep getting 0 colissions.

Then your layer mask is wrong. Overlap sphere will catch any collider overlapping it