Breaking glass on collision at certain velocity

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 :frowning: 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);
}

Unless this javascript compiler is smarter than I suppose, the CompareTag thing is wrong - it should be:

  if (other.CompareTag("player") || other.CompareTag("enemy")){

Also check if the capitalization is correct: the builtin player tag is “Player”.

Now, the speed problem: if player and enemy are rigidbodies, you can read the rigidbody.velocity; if player and enemy are CharacterControllers, you can read the character velocity. There are some problems in this case, however: CharacterController.velocity calculates speed based on the Move or SimpleMove movements; if the character is pushed against the glass, the velocity reported will be incorrect.

The code for rigidbodies could be:

function OnTriggerEnter (other : Collider) {
  if (other.CompareTag("player") || other.CompareTag("enemy")){
    if (other.rigidbody && other.rigidbody.velocity.magnitude > breakVelocity){
      // break glass
    }
}

The code for CharacterControllers could be:

function OnTriggerEnter (other : Collider) {
  if (other.CompareTag("player") || other.CompareTag("enemy")){
    var chr: CharacterController = other.GetComponent(CharacterController);
    if (chr && chr.velocity.magnitude > breakVelocity){
      // break glass
    }
  }
}