Breaking Glass

Click here to download the sourcefiles for this tutorial as a unitypackage


using UnityEngine;
using System.Collections;

public class Break : MonoBehaviour
{
public Transform brokenObject;
public float magnitudeCol, radius, power, upwards;

void OnCollisionEnter(Collision collision)
{
if (collision.relativeVelocity.magnitude > magnitudeCol)
{
Destroy(gameObject);
Instantiate(brokenObject, transform.position, transform.rotation);
brokenObject.localScale = transform.localScale;
Vector3 explosionPos = transform.position;
Collider[] colliders = Physics.OverlapSphere (explosionPos, radius);

foreach (Collider hit in colliders)
{
if (hit.rigidbody)
{
hit.rigidbody.AddExplosionForce(power*collision.relativeVelocity.magnitude, explosionPos, radius, upwards);
}
}
}
}
}

PeerPlayBreaking Glass

Leave a Reply

Your email address will not be published.