- Home /
Collider trigger only affecting a single game object when it should affect multiple
I'm working on a 2D top-down action RPG. I have a fireball spell that instantiates where you click the mouse. It starts small and rapidly expands until it reaches it's maximum size, then destroys itself. I wanted it to both to be a physics collider so that it will push game objects away, and also be a trigger collider to apply damage to them.
My fireball consists of three objects. I have the parent Fireball, which has the fireball script, sprite render, and the animator.
public class Fireball : MonoBehaviour {
public int damage;
public float destroyTimer;
void Update () {
if (destroyTimer > 0.5f)
destroyTimer -= Time.deltaTime;
if (destroyTimer < 0.5f)
Destroy(gameObject);
}
}
There are two children, one of them only contains the physics collider. The other has the trigger collider and this script.
public class FireballTrigger : MonoBehaviour {
private int damage;
public GameObject parent;
private bool damageDone;
void Start () {
damage = parent.GetComponent<Fireball>().damage;
}
void OnTriggerEnter2D(Collider2D collider)
{
Stats stats = (Stats)collider.gameObject.GetComponent("Stats");
if (stats != null && damageDone == false)
{
stats.AddDamage(-damage);
damageDone = true;
}
}
}
Here's where I run into a problem. I set up a scene with a bunch of targets to test it out, but it will only add the damage to one of the targets. I disabled the physics collider to see if it was pushing the objects out of the way of the trigger, but there was no change. How can I get this explosive ball of fire to kill a room full of dudes instead of just one in a crowd?
The first time the fireball collides with anything, damageDone becomes true and no more collisions will result in damage.
You may want to put the damage part on the enemy scripts anyway, as I assume there will be multiple sources of damage in the final game.
Your answer
Follow this Question
Related Questions
Child object's collider (on a different layer) is interfering with parent Physics... 0 Answers
How having correct bounce on a rotating pinwheel(it's more complicated)? 0 Answers
Collider2D/RigidBody2D not working 1 Answer
2D Collision detection not working 0 Answers
Triggers colliding when they are set not to in Collision Matrix 4 Answers