- Home /
Glass break effect
I was wondering if you could make a script in java or c sharp that takes a object and divides it as if it were glass,if it is hit by raycast.Thank you for reading this and have a great day.
in video games it is very common that you: swap an object out, for another version of the object. for example, perhaps the "dead version" or the "broken version" or the "exploded version" or the "high-power version"
often when you're playing a video game, the objects are actually totally swapping out for new ones, even if you don't realize it.
in this example, before hand you make the "normal" object and you make the "shattered" object which will proabbly have a few pieces. to shatter it you just swap from the normal to the "shattered version"
as you say just Destroy the original object (or more likely simply move it offscreen) and instantiate the new object (or more likely simply move it onscreen from your offscreen pool area
As an incredibly advanced topid, as Aldo hints at you could actually re-engineer objects in real time (modifying the mesh and so on, using artificial intelligence to decide on new shapes and what have you) but that it tremendously beyond what you're asking. what you're asking is just an everyday "swap to a broken version" - so enjoy!!
Answer by aldonaletto · Sep 30, 2012 at 09:50 PM
This can be done, since Unity allows access to the mesh, but the results may be deceptive: the mesh division algorithm may take too much time, and also produce too repetitive segments. An intermediate solution could be to create the object with a suitable triangle division, and simply "break" these triangles and convert them to meshes when the object is hit.
But if you want the pieces to fall and bounce like real world objects, forget about this method: you must create a mesh collider for each piece, a slow operation that may cause a hiccup in your game. It's better to previously create the broken version as a prefab and replace the healthy object with it when necessary.
so quickly instantiate the broken object after the one that isn't broken gets hit.
You can instantiate the broken version when the object is hit - it's a simple solution and may work fine enough. But @Fattie is right: when the game becomes too complex, instantiating and destroying objects may cause performance hiccups, thus some pool system is often used. In this particular case, however, the best solution would be to have the broken version already in place, but deactivated: when the glass is hit, deactivate the healthy version and activate the broken one - like this (healthy object script):
var brokenObj: GameObject; // drag the broken version here // glass starts with little health, so that it dies easily private var health: float = 5;
function ApplyDamage(damage: float){ health -= damage; if (health <= 0){ gameObject.SetActiveRecursively(false); // turn off the healthy version... brokenObj.SetActiveRecursively(true); // and turn on the broken one } } NOTE: You must call ApplyDamage in this object when it's hit. You can get the object script and call the function directly, or use Send$$anonymous$$essage, like this
hit.transform.Send$$anonymous$$essage("ApplyDamage", 10, Send$$anonymous$$essageOptions.DontRequireReceiver);
Answer by hvilela · Sep 30, 2012 at 11:30 PM
I recommend you to create two objects. One with one mesh, to use before it gets hit and another composed by several meshes, to replace the first one when it get's hit.