How to use normalize in Rigidbody2D, top-down movement?

I have a simple top down movement script, it should be smooth. The diagonal speed is greater than horizontal/vertical, so I have to normalize it. However, if I use Vector2.normalized or Vector2.Normalize(), movement feels very bad and slippery, like moving on ice …and also snappy, not smooth at all. I don’t know how to use normalize properly in this scenario.

Edit, Solution:

instead of Vector2.Normalize()

movement = Vector2.ClampMagnitude(movement, 1);
public class PlayerScript : MonoBehaviour
{
    Rigidbody2D rb;

    [SerializeField] float movespeed = 10;

    //float smoothSpeed = 0.5f;

    Vector2 movement;
    Vector2 currentInputVector;
    Vector2 currentVelocity;

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

    private void Update()
    {
        float moveX = Input.GetAxis("Horizontal");
        float moveY = Input.GetAxis("Vertical");

        //movement = new Vector2(moveX, moveY).normalized;
        //currentInputVector = Vector2.SmoothDamp(currentInputVector, movement, ref currentVelocity, smoothSpeed);

        movement = new Vector2(moveX, moveY);
        movement.Normalize();
    }
    private void FixedUpdate()
    {
        //rb.velocity = new Vector2(currentInputVector.x * movespeed, currentInputVector.y * movespeed);
        rb.MovePosition(rb.position + movement * movespeed * Time.fixedDeltaTime);

    }
}

Perhaps instead of Vector2.normalize what you’re looking for is Vector2.ClampMagnitude() ??

This seems more appropriate to your use case.

1 Like

Oh… I didn’t thought of that… well I guess, that’s why I am a stupid plank. Thanks. works perfectly.

1 Like