- Home /
airplane collision detection problem
hi their , i am doing a simple top-down view airplane game where u pilot a small airplane then u can land it on the airport , this is where i have the problem bcz the airplane is rigid body here is the scene settings:
1-a flat ground with a box collider(if over this it must crash)
2-a flat rectangle plane (Airport) with a box collider ,if over this it must not crash ever.
3-airplane that is controlled via physics that have box collider that prevents it from falling.
the problem : when landing hard (i want to damage it only) it crashes as if it is over the flat ground , because the colliders intersect with each other and pass each other for a split second.
note:the airport is over the flat ground.
Script:
#pragma strict
var hits : RaycastHit[];
var airport : Collider;
var ground : Collider;
var height : float;
var boomObject : GameObject;
var airplaneObject : GameObject;
var planeWreck : GameObject;
var isLanded : boolean;
function OnGUI(){
if(isLanded == true){
GUI.Box(Rect(Screen.width / 2 - 50 , 10 , 100 , 25),"Landed!");
}
}
function FixedUpdate () {
hits = Physics.RaycastAll (transform.position, -transform.up, 0.5);
for( var hit : RaycastHit in hits ) {
if(hit.collider == airport){
height = transform.position.y - hit.point.y;
if( height <= 0.5){
isLanded = true;
return;
}
}
if(hit.collider == ground){
height = transform.position.y - hit.point.y;
if( height <= 0.5){
Instantiate(boomObject, transform.position, transform.rotation);
Instantiate(planeWreck, transform.position, airplaneObject.transform.rotation);
Destroy(airplaneObject);
Destroy(GetComponent(PlaneControl));
return;
}
}
}
}
please advice and thanks for any help i can get.
Answer by MigLeader · Aug 23, 2013 at 04:44 PM
ok i have fixed it somehow (work around) , by adding a small timer to the crash script:
#pragma strict
var hits : RaycastHit[];
var airport : Collider;
var ground : Collider;
var height : float;
var boomObject : GameObject;
var airplaneObject : GameObject;
var planeWreck : GameObject;
var isLanded : boolean;
var timeToCrash : float;
var startTimeToCrash : float;
function Start(){
timeToCrash = startTimeToCrash;
}
function OnGUI(){
if(isLanded == true){
GUI.Box(Rect(Screen.width / 2 - 50 , 10 , 100 , 25),"Landed!");
}
}
function FixedUpdate () {
hits = Physics.RaycastAll (transform.position, -transform.up, 0.5);
for( var hit : RaycastHit in hits ) {
if(hit.collider == airport){
height = transform.position.y - hit.point.y;
if( height <= 0.5){
isLanded = true;
return;
}
}
if(hit.collider == ground){
height = transform.position.y - hit.point.y;
if( height <= 0.5){
isLanded = false;
TimetoCrash();
//return;
}
}
}
}
function TimetoCrash(){
timeToCrash = timeToCrash - 0.01;
if(isLanded == true){
timeToCrash = startTimeToCrash;
}
if(timeToCrash <= 0){
Instantiate(boomObject, transform.position, transform.rotation);
Instantiate(planeWreck, transform.position, airplaneObject.transform.rotation);
Destroy(airplaneObject);
Destroy(GetComponent(PlaneControl));
timeToCrash = startTimeToCrash;
}
}
i have set the (startTimeToCrash) to 0.1;
i know its kind of hackish way to do it but it did it for me.
if there is any better solution for it i wish somebody post it plz, thx.