i am trying to make a house that has windows that smash when a bullet is fired at it. if it is possible can someone plz give me some sort of tutorial on how to do it because i have tryed everything. Also if you can can you include some sort of script because im usless at writing long script.
UPDATE: Sorry for the wall of text, the update made this longer than intended.
I will assume you have experience in 3D animation, or the ability to google for an animation tutorial for shattering glass. Note that Unity only supports bone animations however (or at least it did in 2.6), so detached bones is your best friend (we used this in a previous game in combination with Havok to blow up rocks).
So, your best bet is to animate the glass shattering using a 3D modeller, like Maya or 3ds Max. Say you model the window as a whole on frame 1, then on frames 5 to 30 you have it falling to pieces. Then export it as an FBX with animations baked, and select the FBX in Unity. The inspector then lets you set up some things related to the FBXImporter. Under Animations, click the little + on the right side of the table to add two new rows. Set them up like below
Name | Start | End | WrapMode | Loop
static 1 1 Once unchecked
destroy 5 30 Once unchecked
Then drag it out and place it wherever you want a window. Give it a parent object with an appropriate collider component (the parent is to avoid breaking the prefab, in case you update the model file). Set the parent's layer to a layer dedicated to windows. Go to Edit -> Project Settings -> Physics and set up the collision matrix for the window layer to only collide with whatever layer the bullets will be in. Then attach something like the following script to the window's parent (with the collider).
using UnityEngine;
public class BreakingWindow : MonoBehaviour
{
private bool isBroken = false;
public void OnCollisionEnter(Collision collisionInfo) {
if (!isBroken) {
Animation anim = (GetComponentInChildren(typeof(Animation)) as Animation);
if (anim != null) {
anim.Play("destroy");
} else {
Debug.Log("Object has no animation component.");
}
isBroken = true;
}
}
}
Basically what happens is, when a collision occurs it tells the first Animation object it finds (which would be the child object's animation in this case) to play the destroy animation.
UPDATE: As posted by Oliver Jones below, this assumes you know beforehand how your ground looks. If you have varying terrain and want the shards to act properly when they land, you should probably have two separate objects. One for the unbroken window, and a prefab which contains the separate shards with rigid bodies attached to them. In this case, you can ignore the animation setup above, and add this code below the isBroken bool;
public Transform brokenWindowPrefab;
public Transform wholeWindowChild;
Then replace the OnCollisionEnter body with;
if (!isBroken) {
Destroy(wholeWindowChild.gameObject);
if (brokenWindowPrefab != null) {
Object.Instantiate(brokenWindowPrefab, transform.position, transform.rotation);
} else {
Debug.Log("Object has no Broken Window Prefab set.");
}
isBroken = true;
}
After that, you select the parent (with the script) and drag its child (the window model) onto the "Whole Window Child" line in the inspector. Then you create your prefab (right click project explorer -> Create -> Prefab) and set it up like any other prefab. Then select the parent again and drag the newly created prefab onto its "Broken Window Prefab" line in the inspector.
What happens is the parent will now destroy the window model when a collision occurs, and create a new instance of the prefab in its place. It will not create the prefab as a child however. I generally prefer having childs of objects be the same layer as the parent, and for the shards that would mean they wouldn't collide with the ground.
It dose not sound like that you need long scripts in this case.
function OnCollisionEnter (hit : Collision)
{
if(hit.gameObject.tag == "Type what ever tag it should be")
{
animation.Play("Type the name of the animation")
}
}
This is what i would do. When your bullet hits the glass tagged "Glass" or something similar. Then play a animation of the glass breaking. You proberly need a modeling software to animate the animation, or you could use Unitys build in animator, but a modeling software is proberly a bit easier to animate in.
Remember to assign the glass a tag. You can do that in the inspector.