I assume your script it’s attached as component of airplane. The airplane must have attached also a Rigibody component.
As Rod Green & vatsalAtTrifekt said, you need somehow the value of Speed to be > 0.0, otherwise your airplane will do nothing.
It’s not clear for me why the airplane need to crash at lower speed and I missed the goal of MaxSpeed variable, but, anyway, if you want, as rules on your script, airplane to crash&explode when Speed it’s lower than 50, then this code may work:
#pragma strict
var explosionPrefab:Transform;
var MaxSpeed = 120;
private var Speed : float = 0.0;
function Start () {
Speed = 40.0;
// If Speed <= 50.0 airplane will explode at impact (by your rule).
// Otherwise will not explode.
// Speed = 40.0; // Will explode
// Speed = 49.9; // Will explode
// Speed = 50.0; // Will not explode
// Speed = 50.1; // Will not explode
// Speed = 100.0; // Will not explode
// Speed = 130.0; // Will not explode (and speed will changed to MaxSpeed)
}
function Update () {
// At each frame, Airplane it's going down with given speed on 'Start' function.
transform.Translate(Vector3.down * Time.deltaTime * Speed);
}
function OnCollisionEnter(col:Collision)
{
if(Speed >= MaxSpeed)
{
Speed=MaxSpeed;
}
if(Speed > 50)
{
return;
}
else
{
var contact : ContactPoint = col.contacts[0];
var rot : Quaternion = Quaternion.FromToRotation(Vector3.up, contact.normal);
var pos : Vector3 = contact.point;
Instantiate(explosionPrefab, pos, rot);
Destroy(gameObject);
}
}
Notes:
‘Translate’ it’s not the best method to use when talk about the speed (see vatsalAtTrifekt comment) but more easy to understand.
The speed of the airplane I added in Start() function as test & learn purpose; I don’t know how you calculate the speed of airplane during of the fly so that I put this value as ‘default’ value of the airplane speed, at the airplane born time; of course, you can test using your:
private var Speed = 40.0;
leaving the Start() function empty.
Airplane movement I added in Update() function; I don’t know how you move the airplane.
Better to use ‘speed’ instead of ‘Speed’ as variable name. I kept the name you use.
Better to define the variable type: private var Speed : float = 0.0;
Use #pragma strict at the beginning of the script; it’s just helping in better coding.
You have to fix what airplane will do if not explode; for the moment the airplane it’s still trying to moving down at speed >=50, even if reached the collider
I have a very basic unitypackage with all above but I don’t know how to attach here.