Instiating In different areas of an object

I would like to have explosions instantiate all around a larger GameObject(A boss). My game is 2d.

Here is what I have for reference.

    public IEnumerator DeathSpawn()
    {
        Instantiate(Explosion, transform.position, Quaternion.identity);
        yield return new WaitForSeconds(.1f);
        Instantiate(Explosion, Fire.transform.position, Quaternion.identity);
        yield return new WaitForSeconds(.1f);
        Instantiate(Explosion, transform.position, Quaternion.identity);
    }

Is there a way to Instantiate explosions all around the boss without having to go in and add Transforms for each area I want an explosion to spawn?

Thanks in advance,
KickaPK

Sorry for title spelling error.

I’d use insideUnitCircle to generate a random vector2, multiply it by the max distance you want the explosion to be from the boss. Then you place the explosion at that position relative to the boss, which I believe all you do is just add it to the boss’s world space position. Repeat each time you want to add another explosion.

If you were doing the same thing in 3D instead of 2D, you’d use insideUnitSphere instead.

It will not let me add it to my Boss’s position.

    public IEnumerator DeathSpawn()
    {
        Vector2 y = Random.insideUnitCircle * 2;
        Instantiate(Explosion, transform.position + y, Quaternion.identity);
        yield return new WaitForSeconds(1f);
        Instantiate(Explosion, transform.position + y, Quaternion.identity);
        yield return new WaitForSeconds(1f);
        Instantiate(Explosion, transform.position + y, Quaternion.identity);
        yield return new WaitForSeconds(1f);
        Instantiate(Explosion, transform.position + y, Quaternion.identity);
        yield return new WaitForSeconds(1f);


    }

What does “It will not let me” mean? If I’m to guess at what you mean, maybe you are getting an error that your addition is ambiguous. If that is the error then just cast “y” to Vector3 in your addition.

Also, you’d want to regenerate the random position between each explosion, or they will all be instantiated at the same location on top of each other.

Assets\Boss1.cs(85,32): error CS0034: Operator ‘+’ is ambiguous on operands of type ‘Vector3’ and ‘Vector2’

I edited my above comment before seeing this. Just change “transform.position + y” to “transform.position + (Vector3)y” and I expect that to fix it.

Thank you so much, works great. I learned a lot here. Why does adding (Vector 3) work?

So the issue is, Unity made both Vector3 and Vector2 able to implicitly convert to each other. But the ambiguous error is saying the compiler in this case doesn’t know which one you want it to end up as when you add them. The variable “y” is a Vector2 and can be converted to a Vector3. transform.postion is a Vector3 which can be converted to a Vector2. So which one do you want the answer to that addition to be? :stuck_out_tongue: So adding (Vector3) before “y” is called casting, or an explicit conversion. It says I want you to take the variable to the right of it and convert it into this type, Vector3 in this case, before doing the addition. So we end up with Vector3 + Vector3 = Vector3 as far as the compiler is concerned, which removes the ambiguity.

More info on casting:
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/types/casting-and-type-conversions