# Limiting maximum speed of rigidbody

I have been searching for a week. I didn´t find anything that worked for me.
So how do you do it ?
Here is my script :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Movement : MonoBehaviour
{
public float sprintSpeed = 5.6f;
public float speed = 2.5f;
public float skok;
public float gravitace;
private float hI;
private Rigidbody2D rb;
public bool jeNaZemi = true;
private bool vPravo = true;
public Vector2 pohybovani;
// Start is called before the first frame update
void Start()
{
rb = GetComponent();
Physics2D.gravity *= gravitace;
}

``````// Update is called once per frame
void Update()
{
hI = Input.GetAxisRaw("Horizontal");
}
private void OnCollisionEnter2D(Collision2D collision)
{
jeNaZemi = true;
}
void Pretocit()
{
vPravo = !vPravo;

Vector3 Scaler = transform.localScale;
Scaler.x *= -1;
transform.localScale = Scaler;
}
private void FixedUpdate()
{
if (Input.GetKey(KeyCode.LeftShift))
{
rb.AddForce(Vector3.right * hI * sprintSpeed);
}
if (Input.GetKeyDown(KeyCode.Space) && jeNaZemi)
{
rb.AddForce(Vector3.up * skok, ForceMode2D.Impulse);
jeNaZemi = false;
}
if (vPravo == false && hI > 0)
{
Pretocit();
}
else if (vPravo == true && hI < 0)
{
Pretocit();
}
}
``````

}

Have you tried `Mathf.Clamp()`? This limits the lengths of a value, which is useful for limiting the Velocity of your rigidbody. For example:

``````public float MaxVelocity;

void Update()
{
rb.velocity = new Vector3
(
Mathf.Clamp(rb.velocity.x, -MaxVelocity, MaxVelocity),
Mathf.Clamp(rb.velocity.y, -MaxVelocity, MaxVelocity),
Mathf.Clamp(rb.velocity.z, -MaxVelocity, MaxVelocity),
);
}
``````

You can also do this using the drag value of your rigidbody component in the editor. @Rostislavvacek116