How can i block sideways movement?

How can i block sideways movement when the player hits a cube? I need only to move on z and x axis.

Intuitively I’d say just take the value that defines your movement before it get’s applied to your GameObject and just “cut” off the value of the axis you want to block. Be it the smaller value, this is an example of what I mean:

//where ever this comes from.. could be your rigidbodies velocity or something
Vector2 velocity = new Vector2(2.0f, 1.0f);

if (velocity.x > velocity.y) {
    velocity.y = 0;
} else {
    velocity.x = 0;
}

Put this in LateUpdate and you’re good to go I think