- Home /
Fast way to make breaking glass ?
Hey guys making a city in unity and was wondering if anyone had a fast way of making glass that can be destroyed. I'm using blender for modeling and I've had a fair bit of experience.
I suggest that you elaborate a bit more on your question before you ask it, this is an extremely vague question which don't really have any answer to it before we know more what you want.
How do you want the glass to be broken? Clicking on it? Throwing snowballs at it?
What is the glass? GameObject? Sprite? Texture?
Do you want to simulate the shattering?
Try add some more info and we'll try to help you :) maybe also add what you have tried so far, but has not worked for you.
Haha sorry the vagueness guys (first question) cheers for all the answers all of these methods are really helpful I'll go try it and let you know!
Answer by Cherno · Apr 07, 2015 at 10:35 AM
Here's a script that will make every gameobject which has a renderer explode into it's triangles.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class TriangleExplosion : MonoBehaviour {
public IEnumerator SplitMesh (bool destroy) {
if(GetComponent<MeshFilter>() == null || GetComponent<SkinnedMeshRenderer>() == null) {
yield return null;
}
if(GetComponent<Collider>()) {
GetComponent<Collider>().enabled = false;
}
Mesh M = new Mesh();
if(GetComponent<MeshFilter>()) {
M = GetComponent<MeshFilter>().mesh;
}
else if(GetComponent<SkinnedMeshRenderer>()) {
M = GetComponent<SkinnedMeshRenderer>().sharedMesh;
}
Material[] materials = new Material[0];
if(GetComponent<MeshRenderer>()) {
materials = GetComponent<MeshRenderer>().materials;
}
else if(GetComponent<SkinnedMeshRenderer>()) {
materials = GetComponent<SkinnedMeshRenderer>().materials;
}
Vector3[] verts = M.vertices;
Vector3[] normals = M.normals;
Vector2[] uvs = M.uv;
for (int submesh = 0; submesh < M.subMeshCount; submesh++) {
int[] indices = M.GetTriangles(submesh);
for (int i = 0; i < indices.Length; i += 3) {
Vector3[] newVerts = new Vector3[3];
Vector3[] newNormals = new Vector3[3];
Vector2[] newUvs = new Vector2[3];
for (int n = 0; n < 3; n++) {
int index = indices[i + n];
newVerts[n] = verts[index];
newUvs[n] = uvs[index];
newNormals[n] = normals[index];
}
Mesh mesh = new Mesh();
mesh.vertices = newVerts;
mesh.normals = newNormals;
mesh.uv = newUvs;
mesh.triangles = new int[] { 0, 1, 2, 2, 1, 0 };
GameObject GO = new GameObject("Triangle " + (i / 3));
GO.layer = LayerMask.NameToLayer("Particle");
GO.transform.position = transform.position;
GO.transform.rotation = transform.rotation;
GO.AddComponent<MeshRenderer>().material = materials[submesh];
GO.AddComponent<MeshFilter>().mesh = mesh;
GO.AddComponent<BoxCollider>();
Vector3 explosionPos = new Vector3(transform.position.x + Random.Range(-0.5f, 0.5f), transform.position.y + Random.Range(0f, 0.5f), transform.position.z + Random.Range(-0.5f, 0.5f));
GO.AddComponent<Rigidbody>().AddExplosionForce(Random.Range(300,500), explosionPos, 5);
Destroy(GO, 5 + Random.Range(0.0f, 5.0f));
}
}
GetComponent<Renderer>().enabled = false;
yield return new WaitForSeconds(1.0f);
if(destroy == true) {
Destroy(gameObject);
}
}
}
Answer by Woody3d7 · Apr 07, 2015 at 01:11 PM
Don't you hate it when people ask questions rather than give ANY useful info.. You might want a Broken Window, Just go into the window mesh (I use 3DS MAX, but should be the same...) and either use the Slice tool(like on MAX) to slice the object however you want (usually in a radial pattern from a center break?) or you could make multiple copies of an object, delete faces on each object so that you end up with a single window again, but on each obj you deleted unique faces, then with either method, animate the pieces in place then key frame them in the destination, add another keyframe or 2 halfway through to introduce a bit of an arc in the pieces movement,.. scale the bigger pieces keys to move slower then the smaller pieces, etc,.
quick and simple.
Robert
Some of my Animation: https://www.youtube.com/watch?v=NCD9tgWeZYQ