How can i detect when the spaceship has landed ?

I want to find when the spaceship has landed and when the landing act finished and then to wait 2-3 seconds and then to do something else.

In the script i set the landing bool variable by default to true just for the testing.

using UnityEngine;
using System.Collections;

public class ControlShip : MonoBehaviour {

    public int rotationSpeed = 75;
    public int movementspeed = 10;
    public int thrust = 10;

    private bool isPKeyDown = false;
    private float acceleration = .0f;
    private Vector3 previousPosition = Vector3.zero;
    Rigidbody _rigidbody;
    private bool landing = true;

    // Use this for initialization
    void Start () {

        _rigidbody = GetComponent<Rigidbody>();
        Debug.Log("Acc Speed: " + thrust);
    }

    // Update is called once per frame
    void Update()
    {
        if (landing == false)
        {
            var v3 = new Vector3(Input.GetAxis("Vertical"), Input.GetAxis("Horizontal"), 0.0f);
            transform.Rotate(v3 * rotationSpeed * Time.deltaTime);
            transform.position += transform.forward * Time.deltaTime * movementspeed;

            if (Input.GetKey(KeyCode.Z))
                transform.Rotate(Vector3.forward * rotationSpeed * Time.deltaTime);

            if (Input.GetKey(KeyCode.R))
                transform.Rotate(Vector3.right * rotationSpeed * Time.deltaTime);

            if (Input.GetKey(KeyCode.P))
            {
                isPKeyDown = Input.GetKey(KeyCode.P);
                float distance = Vector3.Distance(previousPosition, transform.position);
                acceleration = distance / Mathf.Pow(Time.deltaTime, 2);

                previousPosition = transform.position;
                _rigidbody.AddRelativeForce(0f, 0f, thrust, ForceMode.Acceleration);
            }
        }
        else
        {
            if (_rigidbody.velocity.sqrMagnitude < .01 && _rigidbody.angularVelocity.sqrMagnitude < .01)//much faster than magnitude
            {
                Debug.Log("Landed");
            }
        }

        if (Input.GetKey(KeyCode.L))
        {
            landing = true;
        }
    }

    void OnCollisionEnter(Collision col)
    {
        if (landing == true && col.gameObject.name == "Base")
        {

        }
    }

    void OnGUI()
    {
        if (isPKeyDown)
        {
            GUI.Label(new Rect(100, 100, 200, 200), "Acc Speed: " + acceleration);
        }
    }
}

The spaceship object have a rigidbody component and a box collider component.
Also the Base(Cube in red) have a box collider.

What i want to do is to detect/find when the landing is finished and once the spaceship landed to wait 2-3 seconds and then do something after the 2-3 seconds.

I tried to use this part in the script but it’s not working good it’s showing the Debug “Landing” just when running the game.

I attached a youtube short video clip showing the landing and the base and spaceship objects and inspector of them:

One thing I noticed is that you have your collider set to trigger but you are using OnCollisionEnter in the script (instead of OnTriggerEnter).
As for waiting 2-3 seconds, you could Invoke with a delay or run a coroutine which waits the time before changing your “state”… or even just adding a float delay variable which does += Time.deltaTime would suffice for such a thing :slight_smile:

That any help? :slight_smile:

I’m trying to use StartCoroutine but it’s not waiting 10 seconds.

void OnTriggerEnter(Collider other)
    {
        if (landing == true && other.gameObject.name == "Base")
        {
            StartCoroutine(Landed());
            Debug.Log("Landed");
        }
    }

    IEnumerator Landed()
    {
        yield return new WaitForSeconds(10);
    }

Once it’s entering and landing is already true it’s showing the “Landed” before even 1 second passed.

The Log prints out even if you call the Coroutine.
Say you put the print “Landed” after the yield return in the Landed() Method, you would see it after 10 seconds. that’s how it works :slight_smile:

Working perfect :slight_smile: Thank you.

No problem :slight_smile: Cheers.