Velocity X and Y speed limits

I’ve looked around and tried many of the answers but still can’t quite get what I want. I have many gameObject prefabs with rigidBody2d.

I apply a force to get them moving in their physical world but things get out of hand if they go too fast from various collisions. I’m looking to have min and max speed limits for both the x and y velocities (which can be negative).

Ideally the objects should never be able to go horizontally across or vertically up/down because the speed limits will kick in and can’t get stuck stationary.

I first tried:

		if(rb.velocity.magnitude > maxSpeed) {
			float reduction = maxSpeed/rb.velocity.magnitude;
			rb.velocity *= reduction;
		}

		if(rb.velocity.magnitude < minSpeed) {
			float increase = minSpeed/rb.velocity.magnitude;
			rb.velocity *= increase;
		}

But didn’t work. Think vectors are beating my brain up. Thanks for any help.

I suppose you already tried: rb.velocity.x/y = minSpeed/maxSpeed?
Ex.

if(rb.velocity.y < minSpeedY){
Vector2 newVelocity = new Vector3(rigidbody.velocity.x, minSpeedY);
rb.velocity = newVelocity;
}

EDIT: Ok, let’s do the math.
What does Vector2.magnitude do? It gets the magnitude which is the same as the Hypotenuse of the corresponding triangle.
What does multiplying a vector2 do? It multiplies each component by the factor.

So you try to solve the equation sqrt(x*x + y*y)*q = minSpeed.
Let's say minSpeed = 20 and our Vector is (2,1). This will give us the equation: sqrt(4)*q = 20. So q= 20/2 = 10.
When you then multiply your vector you get a vector which is (20,10) the magnitude of this is sqrt(20*20 + 10*10) = sqrt(400 + 100) = sqrt(500) ~= 22.
So you didn't get 20 as you wanted.
What you really want to solve is: 20*20 = x*q*x*q + y*q*y*q.
So if the vector2 was (2,1) this would get us 400 = 4q^2 + q^2 = 5*(q^2). So we would get q = sqrt(400/5) ~= 8.94.
If we now multiply our vector we would get (17.88,8.94) and the magnitude of this would be sqrt(17.88^2 + 8.94^2) ~= sqrt(320 + 79.9) = sqrt(399.9) = 19.99 ~= 20!

Hurray!

So in your code the equation would be:

float increase = Mathf.sqrt((minSpeed*minSpeed)/((rb.velocity.x*rb.velocity.x) +(rb.velocity.y*rb.velocity.y))); //We just break out q from our equation "20*20 = x*q*x*q + y*q*y*q" => "20^2 = x^2*q^2 + y^2*q^2" => "20^2 = (x^2 + y^2)*q^2" => sqrt(20^2/(x^2 + y^2)) = q