- Home /
How can I make the explosions I have on a object spawn on the mesh
I am not trying to destroy the object, I am just looking how to make the "explosion" instances blow up on the outer mesh. I am shooting at it with a gun and just want to see random explosions once in a while. I was thinking a rand type of function but I dont know what function I use to get the GameObject mesh.
Answer by Loius · Jan 03, 2013 at 11:39 PM
You'd have to find a random triangle in the mesh, then find a random point in that triangle.
int triangle = Random.Range(0,mesh.triangles.Length / 3) * 3;
Then your three verices are mesh.vertices[triangle], [triangle+1], and [triangle+2].
There are a few ways to get an object's mesh; the easiest way is probably gameObject.GetComponent().mesh, but check out the script reference too.
Edit - Sigh, and after "GetComponent" you need a Less Than symbol, then "MeshRenderer", then a Greater Than symbol.
So I can put?
function start(){ int triangle = Random.Range(0,mesh.triangles.Length / 3) * 3; }
if (die){ Instantiate(explosionPrefab, transform.position, transform.localRotation); }
Answer by strachpr01 · Jan 04, 2013 at 12:50 AM
you could attach the explosion to the bullet you are firing, you can then get it to explode on impact on anything by instantiating the explosion where the bullet is at the collision then just destroying the bullet.
I already have what you are advising. I need a random explosion on what I am hitting, I already have my instance on my bullet prefab.
Thanks though.
the only thing i could think of would be to use empty game objects on the mesh and use Random.Rage to select the spawn points in a GameObject[] the just instantiate them at the chosen spawn point? its a long way round but could be a solution if no one else answers :)
Yeah I was thinkning that. But the object I need to explode is a prefab. You think that I can get a random position on the outer edge of every prefab?
personally i would make the empty game objects part of the prefab then you can still access them through the GameObject array
That way I can also choose where the explosiuoons dont blow? also can I make the empty game objects in max?