- Home /
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
Answer by GameVortex · Oct 28, 2014 at 08:42 AM
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.
Ah, thank you both! I just shifted my player detection code into line 11, and it worked perfectly. I have another small query though, how can I modify a variable(health) in another gameobject's script? I read that sendmessage isn't that good in terms of efficiency, and I'd like to keep my code running as fast as possible, so is there a good alternative? Thanks again, Weapioneer
if you have Health public variable in your Player script or public method Damage(int damage) to reduce health you may call it like that
hit.gameObject.GetComponent<Player>().Damage(damage);
or
hit.gameObject.GetComponent<Player>().Health -= damage;
I just tried this, and again, it worked perfectly! Thank you for the speedy and useful reply!
Answer by Deadcow_ · Oct 28, 2014 at 09:05 AM
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
Your answer
![](https://koobas.hobune.stream/wayback/20220613170052im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Detecting when Player stoped to push a rigidbody? 3 Answers
Rigidbody Character Jumps Higher With At Least 2 Collisions 3 Answers
Moving colliding Object causes Player to jitter 0 Answers
Play animation when colliding with trigger! 3 Answers
my character always falls when he is walking because of the collider 0 Answers