Unity ignore small vector2 values help

So ive made a character that can move around by changing the velocity of his rigidbody2D component, and have made it so that his rotation is changed depending on the velocity of that rigidbody2D, meaning that if he is moving left but is still sliding right, he will still be pointing right.

The problem occurrs when the player hits a wall, and therefore is moving with very small values away from the wall, resulting in the sprite changing directions every frame.

The way i thought of fixing this is by adding a sort of a deadzone, so that very small values wouldn’t be picked up, but does anyone know how to do this

This is what im using to find the velocity of my rigidbody2D

transform.GetComponent<Rigidbody2D>().velocity;

So it would be like

If (transform.GetComponent<Rigidbody2D>().velocity > "Very low value") {

Make the player point towards traveling direction

}

Although the problem i can see in doing this, is that there will be a delay from when the player starts moving, so if anyone else has a better solution to this, please tell me
Any help is appreciated

You probably want the magnitude of the velocity, not the velocity itself: Unity - Scripting API: Vector3.magnitude

I couldnt get it work well
It basically worked when colliding with the wall most of the time, but, it brought a ton of bugs, where the player would not change rotation if he was close to the 0,0,0 point, or walking in the opposite direction, although the player would rotate into the right direction after a few seconds

            //declare the current Magnitude value for ease of use
            float currentMagnitudeValue = transform.position.magnitude;
            //If delta magnitude is larger than 0.005, then change player rotation
            if (currentMagnitudeValue - lastMagnitudeValue > 0.005)
            {
                //Change rotation to velocity direction
                Vector2 dir = transform.GetComponent<Rigidbody2D>().velocity;
                float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
                transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
            }
            lastMagnitudeValue = currentMagnitudeValue;

Although maybe im not using using the magnitude correctly?

I thought you were trying to suppress rotation if the velocity was very low?

if (rb.velocity.magnitude > 0.1)
{
    // do rotation stuff
}
1 Like

I thought magnitude was a transform.position specific command since im relativly new to the unity commands
Your code fixed most of my problems, although there are still ways of changing directions every frame by sliding on a wall, although to fix this problem i will probably have to come up with a better solution than just creating a deadzone, but this will do for now

Thanks for the help

transform.position and rigidbody.velocity are both Vector3’s which is where magnitude comes from: Unity - Scripting API: Vector3.magnitude

1 Like

Seems ive pretty much fixed the problem completely now!
The second part of my problem was that my hitbox was too complicated which resulted in wierd bugs when using my method of movement and slamming my character against a wall.
Instead i just assigned a simple square as my hitbox (instead of 2 squares, one for head and one for the body), and the problem was pretty much non-existent

Thanks for all the help