Rigidbody controller clamp only horizontal magnitude

I’ve made a rigidbody controller for the game i’m developing, the movement is controlled by forces and not applied directly to velocity because i need a bit of acceleration/deceleration feel while moving and not snappier movements.
I’ve got a Vector3 that store the movement input called worldVec, and i used Vector3.ClampMagnitude to clamp the max velocity my character can move, since i’m using forces.
The problem began when i introduced jump in the controller, in fact Vector3.ClampMagnitude clamp also vertical movements making me unable to jump higher than what i would.

Is there a way to clamp only horizontal ( x and z ) movements?
Or perhaps there’s another way to limit the controller’s velocity without clamping the velocity?

Here’s the code semplified:
Thanks in advance.

Vector3 worldVec;

float jumpSpeed;
float moveSpeed;
float maxSpeed

bool canJump;

void Update()
    {
// Vector input from a virtual joystick i have
worldVec = stick.GetVec3d()

//Jump button is pressed
if(jump)
{
if(IsGrounded())
canJump=true;
}
}

void FixedUpdate()
    {
        if (canJump)
        {

            player.velocity+= new Vector3(0,jumpSpeed,0);
            canJump=false;
        }
                   
//Here i clamp the velocity
        player.velocity=Vector3.ClampMagnitude(player.velocity, maxSpeed);
        player.AddForce (worldVec * moveSpeed * Time.deltaTime);


    }

up

So here you’ll find a bunch of functions I have for vectors (it’s sort of a mix bag, I haven’t organized it nicely yet):

In it you’ll find ‘SetLengthOnAxis’:

        public static Vector3 SetLengthOnAxis(Vector3 v, Vector3 axis, float len)
        {
            axis.Normalize();
            var d = len - Vector3.Dot(v, axis);
            return v + axis * d;
        }

So what you can do is set the length of a vector on some axis… if you set it to 0, then you remove any effect in that direction (note, the axis is arbitrary, you can do this in any direction).

So I would remove the ‘y’ effect, clamp the magnitude, and then add the y back on.

var v = player.velocity;
var yd = Vector3.Dot(v, Vector3.up); //store the magnitude over the up axis
v = VectorUtil.SetLengthOnAxis(v, Vector3.up, 0f);
v = Vector3.ClampMagnitude(v, maxSpeed);
v += yd * Vector3.up;
player.velocity = v;

Or without even using the VectorUtil:

var v = player.velocity;
var yd = v.y;
v.y = 0f;
v = Vector3.ClampMagnitude(v, maxSpeed);
v.y = yd;
player.velocity = v;

Note this one is less calculations because the function I have written is for setting any arbitrary length over any arbitrary axis. I supplied both for you incase you wanted to allow for arbitrary directions of “up”… I use this generalized version myself because a lot of game ideas I have are games with arbitrary “up” axes.