Can I assign a collider to an explosion? If not, how can i detect if the player is inside an explosion radius?

Hi there happy Unity fellows :slight_smile:

I think the question title actually covers it all pretty well.

Im making a driving game, there are enemies which spawn on the side of road and when my player car gets close, they fire a rocket in front of the player. I need to detect if the car is within a certain radius of the explosion centre. So when I can detect that Iโ€™m assuming I can then use rb.AddExplosiveForce to make the car fly a bit or something :slight_smile:

But I cannot figure out how is the recommended way to get the blast radius collider (or equivilent)

Thanks for any help as usual

Hey, you need to make with the explosion a collider that can detect the players, objects and etc there is a good video for you, watch it Grenades and Explosions in Unity 3d

You could probably make this work with a collider, but if youโ€™re just checking within a radius, you could also consider checking the distance between the two GameObjects. That would look something like this:

	void Update ()
    {
        GameObject car;
        GameObject explosion;
        float radius = 10;
		if(Vector3.Distance(car.transform.position, explosion.transform.position) < radius)
        {
        }
	}

Although if your really want to do it using a collider, you can just add a sphere collider to the gameObject with the explosion, and add a script similar to this to the car:

    private void OnTriggerEnter(Collider other)
    {
        if(other.gameObject.tag == "Explosion")
        {
            //Add Force
        }
    }