overlapsphere not detecting player

Hello all,

So I started using unity recently, with about 2 years basic experience in java coding. I am currently trying to make a game, but I have a problem about making an explosion damage the player. The player is made using the FPS character controller, with a “player” tag. My explosion code snippet is below,

void explosion()
	{

		Collider [] colli = Physics.OverlapSphere (Vector3.zero, explosionSize);
		foreach(Collider col in colli)
		{
			if(Physics.Raycast (transform.position, (col.transform.position - transform.position), out hit, explosionSize))
			{
				if(hit.rigidbody == null)
				{ 
					continue;
				}
				else
					hit.rigidbody.AddExplosionForce(explosionForce, gameObject.transform.position, explosionSize, 1, ForceMode.Impulse);
				if(hit.transform.CompareTag("Player"))//player detector
				{
					Debug.Log("ouch");
					HealthScriptRef.Health = HealthScriptRef.Health - (100 - (hit.distance * 2)); 
				}
				else
					Debug.Log (hit.transform.tag.ToString());
				Debug.Log (col.gameObject.tag.ToString ());
			}

		}
		Instantiate (explosionFireball, transform.position, Quaternion.identity);
		DestroyObject(gameObject);
	}

So the explosion uses an overlapsphere to detect all colliders in range, and using raycasting to determine LOS, in order to make using cover useful. This works well with test rigidbody targets, but the code inside the IF statement for detecting players doesn’t run, and the player is never listed in the following ELSE statement tag list. When the third debug statement lists all the colliders, the player is still not mentioned, which makes me think that it might be because the overlap sphere doesn’t include the player in the array. So I gave the player a capsule collider, but no luck, it still doesn’t detect the player. I tried giving the player a rigid body, and it completely glitched up the physics engine, causing the player to fly upwards high.
So I am now completely stumped as to what to do, so I posted this question, please assist.

Yours sincerely,
Weapioneer

On line 11 you are skipping everything and continuing to the next in the list when the target does not have a rigidbody, and as you stated your player does not have a rigidbody.

You should restructure a little and instead check if there is a rigidbody, and add the explosion force if it is. Then check for the player if there is no rigidbody.

Yep, just put

if (hit.tag != "Player") continue;

instead of

if(hit.rigidbody == null)
{ 
    continue;
}

By the way, instead of hit.transform.tag.ToString() you may just use hit.tag
since tag is already a string and hit have such variable you don’t need to refer to transform