ClampMagnitude for x and y

How do I edit the ClampMagnitude line to be able to have separate values for the x and y axis?
Currently I have the one variable maxVel being put into it, but I would like to set separate values for x and y.

public float xmaxVel =10f;
public float ymaxVel =10f;
//public float maxVel = 10f;

void Update (){
GetComponent<Rigidbody2D>().velocity = Vector3.ClampMagnitude(GetComponent<Rigidbody2D>().velocity, maxVel);

Thanks!

Mathf.Clamp(…) can be used on each element in the Vector3

Rigidbody rb = GetComponent<RigidBody2D>();
Vector3 vel = rb.velocity;
rb.velocity = new Vector3(Mathf.Clamp(vel.x, 0f, 10f), Mathf.Clamp(vel.y, 0f, 10f), Mathf.Clamp(vel.z, 0f, 10f));
1 Like

TY! Its working exactly how I wanted.