- Home /
Aoe (Area of effect) only hits one at a time instead of all that have been hit
Hello Everyone, I tried to create a "field of damage" with a Physics.OverlapBox. Tracking of multiple colliders is possible with my code and i can do damage at these. But only one at a time. If one died, the next target gets damaged but i want that all targets within the Physics.OverlapBox gets damaged at the same time.
[RequireComponent(typeof(CharacterStats))]
public class PlayerCombat : MonoBehaviour
{
bool m_Started;
public LayerMask m_LayerMask;
public GameObject Cube;
public float attackSpeed = 1f;
private float attackCooldown = 0f;
public float attackDelay = .6f;
public event System.Action OnAttack;
CharacterStats myStats;
private void Start()
{
myStats = GetComponent<CharacterStats>();
m_Started = true;
}
private void Update()
{
attackCooldown -= Time.deltaTime;
}
public void Attack(CharacterStats targetStats)
{
if (attackCooldown <= 0f)
{
StartCoroutine(DoDamage(targetStats, attackDelay));
if (OnAttack != null)
{
OnAttack();
}
attackCooldown = 1f / attackSpeed;
}
}
IEnumerator DoDamage(CharacterStats stats, float delay)
{
//Use the OverlapBox to detect if there are any other colliders within this box area.
//Use the GameObject's centre, half the size (as a radius) and rotation. This creates an invisible box around your GameObject.
Collider[] hitColliders = Physics.OverlapBox(Cube.transform.position, Cube.transform.localScale / 2, Quaternion.identity, m_LayerMask);
int i = 0;
//Check when there is a new collider coming into contact with the box
while (i < hitColliders.Length)
{
//Output all of the collider names
Debug.Log("Hit : " + hitColliders[i].name + i);
//Increase the number of Colliders in the array
i++;
yield return new WaitForSeconds(0);
stats.TakeDamage(myStats.damage.GetValue());
}
}
void OnDrawGizmos()
{
Gizmos.color = Color.red;
//Check that it is being run in Play Mode, so it doesn't try to draw this in Editor mode
if (m_Started)
//Draw a cube where the OverlapBox is (positioned where your GameObject is as well as a size)
Gizmos.DrawWireCube(Cube.transform.position, Cube.transform.localScale);
}
}
And this is my CharactersStats class: public class CharacterStats : MonoBehaviour { public Slider myhealthBar;
public int maxHealth = 100;
public int currentHealth { get; private set; }
public Stat damage;
public Stat armor;
void Awake()
{
currentHealth = maxHealth;
}
void Update()
{
if (Input.GetKeyDown(KeyCode.T))
{
TakeDamage(10);
}
}
public void TakeDamage(int damage)
{
damage -= armor.GetValue();
damage = Mathf.Clamp(damage, 0, int.MaxValue);
currentHealth -= damage;
Debug.Log(transform.name + "takes" + damage + "damage.");
Debug.Log(transform.name + "at health" + currentHealth);
if (currentHealth <= 0)
{
Die();
}
}
public virtual void Die ()
{
// Die in some way
// This method is meant to be overwritten
Debug.Log(transform.name + "died.");
}
}
Does your script output all the correct collider names?
The main thing I see with your script is that you never get the CharacterStats component from the hit colliders. Each time you call DoDamage it calls TakeDamage on the same CharacterStat, once for each collider hit. It looks like you may need something like:
var hitStats = hitColliders[i].gameObject.GetComponent<CharacterStats>();
Your answer
Follow this Question
Related Questions
Multiplayer do damage 0 Answers
Need help implementing a damage reduction 1 Answer
continuous damage 1 Answer
Trigger and ennemy damage 2 Answers