Y velocity going crazy when landing on a surface.

Hey there, so I have this rocket ship (It’s just a triangle right now). When it lands on a surface It’s Y velocity becomes something like “7.806205e-09” and never stops even though I’m not moving. I really need help sorting this out because I’m checking if the velocity magnitude is less than “0.9f” and if it is then I debug.log “Landed” however that never happens because of this issue.

My ship code:

{
    Rigidbody2D rb;

    [SerializeField]
    private float _upwardsForce;
    [SerializeField]
    private float _fuel;
    [SerializeField]
    private float _currentVelocity;

    [SerializeField]
    private Text _fuelText;
    [SerializeField]
    private Text _velocityText;

    private void Start()
    {
        rb = GetComponent<Rigidbody2D>();
    }

    private void Update()
    {
        _fuelText.text = "Fuel: " + _fuel.ToString("0.0");
        _velocityText.text = "Velocity: " + rb.velocity.ToString("0.000");

        _currentVelocity = rb.velocity.magnitude;

        //RotateModule();
    }

    private void FixedUpdate()
    {
        UpwardsForce();
    }

    private void UpwardsForce()
    {
        if (Input.GetKey(KeyCode.W))
        {
            if (_currentVelocity > 3.5f)
                return;

            if (_fuel > 0)
            {
                rb.AddForce(transform.up * _upwardsForce, ForceMode2D.Force);
                _fuel -= 0.5f;
            }
        }
        else if (Input.GetKey(KeyCode.A))
        {
            if (_currentVelocity > 2.5f)
                return;

            if (_fuel > 0)
            {
                rb.AddForce(-transform.right * _upwardsForce, ForceMode2D.Force);
                _fuel -= 0.2f;
            }
        }
        else if (Input.GetKey(KeyCode.D))
        {
            if (_currentVelocity > 2.5f)
                return;

            if (_fuel > 0)
            {
                rb.AddForce(transform.right * _upwardsForce, ForceMode2D.Force);
                _fuel -= 0.2f;
            }
        }
    }

    private void RotateModule()
    {
        if (Input.GetKey(KeyCode.Q))
        {
            transform.rotation *= Quaternion.Euler(0f, 0f, 1f);
            _fuel -= 0.1f;
        }
        else if (Input.GetKey(KeyCode.E))
        {
            transform.rotation *= Quaternion.Euler(0f, 0f, -1f);
            _fuel -= 0.1f;
        }
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.CompareTag("Surface"))
        {
            if (rb.velocity.magnitude < -1.4f)
            {
                Debug.Log("Safely Landed!");
            }
            else if (rb.velocity.magnitude > -1.6f)
            {
                Debug.Log("Crashed!");
            }
        }
    }
}

Uh… 7.806205e-09 is less than 0.9f. Quite a lot less. Almost 0.9f less (as 7.806205e-09 is very very close to zero).

So I think the problem isn’t what you think it is. Go back and focus on why your Debug.Log isn’t firing when it should.

1 Like

Hey, sorry for the late reply. I’ve figured it out. Thanks!

What was your issue, for us interested readers? I like your avatar image, by the way :slight_smile:

2 Likes

Haha thank you, big fan of Mars! The issue was actually due to a weird rotation bug that happened whenever I land. So I just froze the rotation on the rigidbody component and manually code it through script. And I added a trigger collider slightly above the surface to detect the velocity rather than detecting it through normal collisions which seemed to better. :slight_smile:

hey, nice looking Lunar Lander game!

Thank you! It was for a game jam :slight_smile: