Rigidbody2D.velocity affects the y-axis, but not the x-axis.

I have it set to add velocity to my player whenever I contact a damaging surface, yet only the y-value is changed (the x-value of the player isn’t locked). How come this is happening?

using UnityEngine;

public class Damage : MonoBehaviour
    public HealthManager HPscript;
    private Rigidbody2D rb;
    public float xKnockback;
    public float yKnockback;
    void Start()
        GameObject HPmgr = GameObject.Find("HealthManager");
        HPscript = HPmgr.GetComponent<HealthManager>();
        rb = gameObject.GetComponent<Rigidbody2D>();

    void OnCollisionEnter2D(Collision2D other)
        string tag = other.gameObject.tag;
        if (tag == "one")
            HPscript.currentHP -= 1;
            rb.velocity = new Vector2(xKnockback, yKnockback);


Well, since I can’t see the code which you are using to move the player, I can’t say much. But probably what is happening is that you’re somewhere in it setting the x velocity every frame. So for example, you have something like:

rb2d.velocity = new Vector2(userVelocity, rb2d.velocity.y);

That is run every frame in FixedUpdate/Update. If so, you’re basically locking the X movement.