Detect Successful Landing

I’m trying to detect if a ship’s left and right landing gear are on the landing pad, indicating a success or landing failure (if one landing gear collides but not the other).

Each landing gear is its own game object and they both have their own box colliders -all within a prefab

Problem is both conditions (landing success and landing failure) end up evaluating to true.

  • usingUnityEngine;
  • usingSystem.Collections;
  • usingSystem.Collections.Generic;
    • public classCollision : MonoBehaviour {
    • boollandingGearLeftOnPad = false;
  • boollandingGearRightOnPad = false;
      • voidStart () {}
    • Update
  • voidUpdate () {
    • }
      • voidOnCollisionEnter2D (Collision2Dcoll) {
    • if (coll.gameObject.tag == “LandingGearLeft”){
  • landingGearLeftOnPad = true;
  • //Debug.Log(“LeftLandingGearonPad”);
  • }
    • if (coll.gameObject.tag == “LandingGearRight”){
  • landingGearRightOnPad = true;
    • }
    • if (landingGearLeftOnPad && landingGearRightOnPad){
  • Debug.Log(“Landed Successfully”);
  • }
    • if (landingGearLeftOnPad && !landingGearRightOnPad){
  • Debug.Log(“You missed the landing pad”);
  • }
    • if (!landingGearLeftOnPad && landingGearRightOnPad){
  • Debug.Log(“You missed the landing pad”);
  • }
          • }
    • voidOnCollisionExit2D (Collision2Dcoll){
    • landingGearLeftOnPad = false;
  • landingGearRightOnPad = false;
    • }
  • }

use two boolean methods like 2dcollierwheel.istouching

instead of relying on the event callback

so like if(wheelLeft.isTouching(landingpad layer) && wheelRight.isTouching(landingpadLayer)){
Success;
}
else
{
Crash:
}

1 Like

@frankrs
Thank you for your response. I could use a little more clarification. Your wheelLeft is a gameObject, correct?

I’m having difficulty adding .isTouched to my landing gear (gameObject).

And for some reason, I can’t post code. In the reply post, I tried the icon to right of the film icon but the forum seems to think it is spam…

no, WheelLeft is a collider2d

I think you want to break this down a bit. The way you have it written, you will always get at least one false positive because one landing gear will always fire its collision event first.

You might want to keep the OnCollisionEnter/Exit functions simple and only have them set/clear a true/false variable.

Then in your Update() loop you would check if both landing gear are down for (say) more than a half second, and then say “yeah you landed,” otherwise you crash.

Of course if it is a spaceship and I can come down and add thrust gently to just keep one leg off the ground and the other on the ground, that would still mark me as crashed after the time expires.

Instead you may wish to consider ringing your landing pad with a differently-tagged collider that is essentially “deadly.” If a gear ever touches that, BOOM

Then you can add as many gear as you want and every gear that is landed is tallied up (again in your Update()) and once the number landed equals the number in total, you would consider it landed.

BTW, I dealt with this precise landing skid problem in my Impulse One game here:

http://www.plbm.com

It’s only Android now, but coming soon to iOS.

The problem seems to be, that in my OnCollisionEnter2D, it only detects one of the two box colliders. The prefab has two game objects which are used to hold the box colliders. It will detect one or the other but not both…