Obtener colisiones para múltiples objetos sin las devoluciones de llamada de colisión

I'm making a platformer (sidescroller), and right now I'm making a grenade-launcher for the player.

It spawns grenades which is a prefab, this prefab has a Circle Collider 2D which is the blast radius for the explosion.

When a grenade is spawned I run this (amongst other things);

        // Add the initial force
    rBody.AddForce(new Vector2(forceX, forceY) *300f);
    Invoke("Explode", 2.5f);

I'm having trouble figuring out how I should handle the Explode function. I would like to find all the GameObjects that are colliding with the Circle Collider 2D at that point, but I can't find a way to do it.

I would like to be able to do something like this (not real code but you understand what I'm trying to do)

void Explode () {
            collidingObjects = circleCollider.getCollisions();
            foreach(collidingObject as entity) {
                if(entity = 'player')
                     player.pushLeftOrRight() 
                elseif(entity = 'enemy')
                     grenade.dealDamage(grenade.damage)
            }
    Debug.Log("Explode");
    Destroy(gameObject);
}

I'm guessing that it wouldn't work so can anyone point me in the right direction?

preguntado el 28 de mayo de 14 a las 13:05

1 Respuestas

In your grenade's script, use OnTriggerEnter or OnCollisionEnter to log collisions; basically just have a HashSet for each grenade that is updated and keeps track of what it is colliding with. Then Explode just has to iterate through this set when you call it.

contestado el 29 de mayo de 14 a las 07:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.