- Home /
make multiple gameobjects flyoff in all direction after instantiating ?
i want to instantiate 10 cubes at a location and for that i am using for loop
private void Update()
{
if (Input.GetKeyDown("z"))
{
for (int i = 0; i < 10; i++)
{
Instantiate(Cubee, transform.position, Quaternion.identity);
}
}
}
cubes has rigidbody so they fly off after instantiating because 2 rigidbodies cant stay in same location
now i want them to fly off in all direction, like a explosion but they fly off only horizontally like shown in this video
how do i make them fly off in all the direction ? like a particle system with spherical collider
Answer by stektpotet · Mar 31, 2020 at 05:26 PM
I'd like to add to @unity_ek98vnTRplGj8Q 's answer and point you along to Random.onUnitSphere which is a good starting point for sampling directions you could use in your velocities for the different objects.
So in addition to instantiating the objects in the loop, you'd go through each of their respective rigidbodies and set its velocity to be something like
cubeRigidbody.velocity =
Random.onUnitSphere * Random.Range(minExplosionVelocity, maxExplosionVelocity);
Answer by unity_ek98vnTRplGj8Q · Mar 31, 2020 at 05:00 PM
Using the fact that rigidbodies repel one another to get an explosive effect does not give you a lot of control over the explosion. I suppose you could try randomizing the starting position of each cube by just a little in various directions and it may give you the effect you want, but if you want control over the explosion effect I recommend disabling collision between the cubes and then giving them each a random starting velocity.
Answer by thomasfriday · Jul 12, 2021 at 06:06 PM
Here's a short Youtube video that covers exactly how to explode a cube: https://youtu.be/wcQqYOSteSs

I just watched this video and it is very informational and organized. Even though I didn't really need the information. One day I might.
Your answer
Follow this Question
Related Questions
How can I instantiate rigid bodies on top of each other without them exploding away? 2 Answers
Problems with spawning items that the players can pick up - Unet 0 Answers
Instantiate for loop skips some objects 1 Answer
NullReferenceException: Instatiate with construtor OR in Inspector 1 Answer
I want to respawn the food everytime the snake collides with it 0 Answers