RigidBody 2D's showing incorrect values for velocity

I am using rb.velocity in my scripts to make my character move.
So when I use the :-
rb.velocity = new Vector2(50, rb.velocity.y);

it causes my rigidbody2 inspector to show a y velocity of 7.167013E-06

1 Like

This seems consistent with floating point (im)precision. Gravity is pushing down on the rigidbody, the collider below is “stopping” it and the result is a very low number that should be 0 but it’s 0.000007167013.

Oh ok that kind of makes sense but another thing is that I have a ladder in my platformer(the white sprite shape that you see in the above screenshot). When my player is on it, it’s supposed to have a velocity of 0 if no key is pressed.
The Rigidbody2D shows the velocity to be 0, but my player keeps sliding down instead of having a y velocity 0.
These are 2 consecutive frames of my gameplay:-

7496963--923294--ss 2.png

7496963--923297--ss 3.png

This is the code used for climbing behaviour:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class Climb : MonoBehaviour
{
    public float climbSpeed;
    int climbDirection = 0;
    public Rigidbody2D rb;
    bool canClimb = false;
    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        climbDirection += Convert.ToInt32(Input.GetKeyDown(KeyCode.UpArrow)) - Convert.ToInt32(Input.GetKeyUp(KeyCode.UpArrow)) - Convert.ToInt32(Input.GetKeyDown(KeyCode.DownArrow)) + Convert.ToInt32(Input.GetKeyUp(KeyCode.DownArrow));
        if (canClimb)
        {
          
           rb.velocity = new Vector2(rb.velocity.x, climbDirection * climbSpeed);
         
        }
        Debug.Log(canClimb);
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if(collision.gameObject.tag == "ladder")
        {
            canClimb = true;

        }
    }

    private void OnTriggerExit2D(Collider2D collision)
    {
        if (collision.gameObject.tag == "ladder")
        {
            canClimb = false;

        }
    }
}

You’ll need to debug it yourself, we don’t have the project but I see you using gravity so why would it not be that?

1 Like

I have literally tried everything I could but I can’t seem to find the reason of this happening.
I have disabled every other object in my scene except my player and main camera
disabled all of it’s scripts except transform, rigidbody2d, sprite renderer and this little script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Test : MonoBehaviour
{
    Rigidbody2D rb;
  
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
    }

  
    void Update()
    {
        rb.velocity = new Vector2(0, 0);
    }
}

The rigidbody 2d is showing a x and y velocity of 0 but my player is still falling down slowly
This is my rigidbody properties

UPDATE: The problem stops from happening when I set the gravity scale to 0 and the rate of falling is directly proportional to gravity scale. Is there any way I can stop this from happening without setting my gravity scale to 0 as I want my player to be affected from gravity while not on the ladder

Set the scale to zero while he’s on the ladder, restore it to whatever nonzero value you want when he leaps off.