Spherecast never picks up player?

Hey all, I have a weird bug? when trying to spherecast and find my player. I have the player set to a specific layer, and is within the distance of the spherecast but it never finds the player.

RaycastHit hit;
if (Physics.SphereCast(sightDetector.position, attackRange, Vector3.zero, out hit, 0, playerLayer))
{
     Debug.Log("if");
}
else
{
     Debug.Log("else");
}

so with this code, as I understand it, I should be showing the “if” debug.log if my player is within the attackRange. the player is definitely within attackRange and has capsule collider so should be seen. Any ideas why my player isn’t getting picked up?

@Corva-Nocta

Are you inverting your playerLayer bitmask on purpose?

I tried it both ways and they come back the same. just happened to copy over the inverted code haha. Was trying anything

I think I finally found the answer. Turns out my player’s layer was being changed at runtime to a different layer. Would never have noticed outside of running the game and checking.

Edit: eh no, it is still doing the same. I can’t get any of the “if” code to run

I did a quick test. I think the issue might be this…

Physics.SphereCast(sightDetector.position, attackRange, Vector3.zero, out hit, 0, playerLayer))

Seems like you are using SphereCast like overlap test, when it actually is described in manual as sort of “thick” raycast.

So in this case, if your target is already inside the ray radius (think it like short ray that looks more like sphere at this point) it doesn’t detect the other object. However if you make your ray radius smaller, it will.

You can test this and you’ll see. As soon as ray radius is equal to or slightly bigger than distance to your target, it no longer gets detected.

Maybe try overlap sphere instead as it seems to be what you actually need?

Hm interesting, maybe I have my numbers a bit off. I will try changing the radius to see how that works for me.

And yes I am using it like an overlap sphere, but I assumed overlap sphere would be overkill since I will only ever be looking for a single gameobject, the player. I suppose I could change to overlap sphere which I am fairly certain it will work, despite not needing a list of colliders.

@Corva-Nocta

“I assumed overlap sphere would be overkill since”

Well you are not doing raycast either, you are doing sphere cast, so why would you expect overlap sphere is somehow overkill compared to sphere cast?

But anyway you already described your test area as “attackRange”, you have zero length cast, and no direction so it doesn’t make sense to me, why you would use Sphere Cast instead of OverlapSphere to begin with… as you already are essentially doing an overlap test.

My understanding was that a spherecast would just get the first thing it hit, which would only ever be the player, so it seemed easier. I am quickly finding out that I was very wrong and did not quite understand how the spherecast works in actuality. I think I will just have to go with an overlap sphere, they don’t seem too complicated after looking over the code a bit harder

@Corva-Nocta

My understanding was that a spherecast would just get the first thing it hit, which would only ever be the player, so it seemed easier.

Well to me it already seemed you had isolated your player in your “playerLayer” ? So unless your enemies or whatever are in the same layer, it isn’t a problem… simply use that same layer to only detect if player is in range.

i think it would work if u use LayerMask.GetMask(“playerLayer”) instead of playerLayer. You must write the layer name that the object has on inspector.

RaycastHit hit;
if (Physics.SphereCast(sightDetector.position, attackRange, Vector3.zero, out hit, 0, LayerMask.GetMask("playerLayer")))
{
     Debug.Log("if");
}
else
{
     Debug.Log("else");
}

@CoolCosmos

“You must write the layer name that the object has on inspector.”

Did you actually replicate the case I explained to @Corva-Nocta ? I don’t see how that would change anything.

Also, I can’t see the full code, but I suspect he is using LayerMask playerLayer; but anyway… setting layer in different manner doesn’t change the fact that radius will cause problems.