I am using the following code to determine a hit with either the player (tagged “player”), or an enemy (tagged “enemy”). Further, I am trying to have this triggered only when the player/enemy are moving at a certain speed eg. running but not walking, so as to not have the glass crack and shatter unrealistically if only tapped.
At this stage I can’t get the OnTriggerEnter to work during any collision at all Here is a link to the original example. This is by far the best and most complete example of destructible glass in Unity that I have found (and I have searched here and the forums a very long time believe me), so anyone feel free to use it however you like. All credits to drJones for making this available. This example however only works when the glass is shot at, and does not include velocity detection. What have I done wrong?
Updated Script:
var glassPane : GameObject;
var glassCrack : GameObject;
var glassPieces : GameObject;
var breakVelocity: float = 10;
private var hitAlready : boolean = false;
function Start ()
{
glassCrack.active = false;
hitAlready = false;
}
function OnTriggerEnter (other : Collider) {
if (other.CompareTag("player") || other.CompareTag("enemy")){
var chr: CharacterController = other.GetComponent(CharacterController);
if (chr && chr.velocity.magnitude > breakVelocity){
Debug.Log ("Glass was hit");
// break glass
if (hitAlready == false)
{
glassCrack.active = true;
glassCrack.audio.Play();
hitAlready = true;
}
else
{
Destroy (collider);
Smashed();
}
}
}
function Smashed ()
{
Destroy (glassPane);
Instantiate(glassPieces, transform.position, transform.rotation);
Destroy (glassCrack);
BroadcastMessage ("BreakGlass", SendMessageOptions.DontRequireReceiver);
Destroy (gameObject, 3.0);
}