Question by
DaresFireson · Apr 23, 2019 at 07:31 PM ·
c#collisioncollidercollision detectionsphere
[SOLVED]OverlapingSphere damage only one enemy
I have this code:
private Enemy targetEnemy;
private string enemyTag = "Enemy";
public int spellDamageOverTime = 500;
public int spellDamageRadius = 20;
[HideInInspector]
public Transform thisSpellTarget;
public GameObject spellTarget;
void Update ()
{
if(thisSpellTarget != null)
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitInfo;
if (Physics.Raycast(ray, out hitInfo))
{
thisSpellTarget.transform.position = hitInfo.point;
}
GameObject[] enemies = GameObject.FindGameObjectsWithTag(enemyTag);
foreach (GameObject enemy in enemies)
{
targetEnemy = enemy.GetComponent<Enemy>();
}
}
if (Input.GetMouseButtonDown(0) && thisSpellTarget != null)
{
//Debug.Log("Mouse clicked");
Destroy(thisSpellTarget.gameObject);
StartCoroutine(SpellDOT());
}
}
public void SpellButtonAction()
{
thisSpellTarget = ((GameObject)Instantiate(spellTarget)).transform;
}
private IEnumerator SpellDOT()
{
Collider[] colliders = Physics.OverlapSphere(thisSpellTarget.transform.position, spellDamageRadius);
foreach (Collider collider in colliders)
{
if (collider.tag == "Enemy")
{
//Debug.Log(targetEnemy);
//Debug.Log(spellDamageOverTime * Time.deltaTime);
targetEnemy.TakeDamage(spellDamageOverTime);
}
}
yield return new WaitForSeconds(3f);
}
It only damage 1 enemy at the margin of the sphere and ignore the others. Also
spellDamageOverTime * Time.deltaTime
return 6.1
Comment
Best Answer
Answer by DaresFireson · Apr 24, 2019 at 10:55 AM
I had to add
targetEnemy = collider.GetComponent<Enemy>();
inside the collider if statemen
Your answer
Follow this Question
Related Questions
Ignore collision based on position 1 Answer
How to make an object that collides with just the player? 1 Answer
Collision With Text 0 Answers
Collision or Triggers both not working. 3D. 1 Answer
How To how ?? 2 Answers