- Home /
Other
Random results with physics.OverlapSphere and AOE attacks
Hi, I am using the following code to create an area of affect magic spell.
It works fine, but seems to have some random results in hit detection. For example, I can see enemies within range, I trigger this and nothing happens, trigger it a second time and they all die.
Sometimes it triggers the first time, sometimes the third.
Can anyone look at this and find out why this seems to have seemingly random results?
if (playerInventory.playerOneMagicPotions >= 1 && magicAttackInProgress == false)
{
magicAttackInProgress = true;
speechPlayerScript.speech.Say("Using Spell", 3);
Instantiate(magicParticle, transform.position, transform.rotation);
Collider[] cols = Physics.OverlapSphere(transform.position, -20.0f);
foreach (Collider col in cols)
{
if (col.tag == "Enemy")
{
ScriptEnemyDamage e = col.gameObject.GetComponent<ScriptEnemyDamage>();
if (e != null)
{
e.TakeDamage(playerActualAttributes.playerOneActualMagicDamage);
}
}
}
yield return new WaitForSeconds(3.0f);
magicAttackInProgress = false;
}
Any help would be greatly appreciated.
Put some Debug.Log() statements in here to deter$$anonymous$$e:
Are you making it to line 5?
How many colliders are found in the OverlapSphere() call?
Is 'e' ever null?
Are you calling into the coroutine more often than 3.0 seconds (i.e. are you stacking up coroutines)?
Any chance that 'playerActualAttributes.playerOneActual$$anonymous$$agicDamage' could be 0.0?
Thanks for the advice Roberbu, I feel a bit stupid now, as it was working perfectly and I couldn't find any errors in my code at all, until hours later it hit me that the enemy was healing itself.... sigh, I really should remember what I have coded into my own game lol :D
Follow this Question
Related Questions
Detect collider collision ,identify and access colliding colliders 0 Answers
Physics.OverlapSphere not detecting Instantiated Objects 1 Answer
Does OverlapSphere() return the colliders in any particular order? 1 Answer
overlapsphere to destroy NPCs on exit 1 Answer
Trying to use colliders on trees to interact with player 1 Answer