Noob question for 2D velocity.x

Here is my code right now

void Update () {
	if(rb2d.velocity.magnitude > maxSpeed)
	         {
	                rb2d.velocity = rb2d.velocity.normalized * maxSpeed;
	         }
}

I want to modify it such that it only controls velocity in the x-direction. Changing my code to

void Update () {
	if(rb2d.velocity.x.magnitude > maxSpeed)
	         {
	                rb2d.velocity.x = rb2d.velocity.x.normalized * maxSpeed;
	         }
}

returns an error. However, velocity.x is valid. I don’t know what’s wrong.

velocity is a Vactor2, and x is a float. float doesn’t have a magnitude property, like Vector2 has.

Solution, however, is very simple. You don’t need magnitude for one dimension. It is already a scalar.

if(Mathf.Abs(rb2d.velocity.x) > maxSpeed)
{
 rb2d.velocity = new Vector2(maxSpeed * Mathf.Sign(rb2d.velocity.x), rb2d.velocity.y);
}