Opposite Rigidbody force

Hey People,
I have a problem with the Rigidbody.
I want use it for move main player…
But, I want to set the max velocity…
I want create one opposite force to set constant velocity when velocity are going to max value,
but in my code don’t work.
Ty for the help, and sorry for my bad english.

public class basicMovment : MonoBehaviour {

private Rigidbody m_CharacterController;
private Vector3 m_MoveDir;
private Vector3 j_MoveDir;
public float strong;
public float strong2;
public float slowDownSpeed;
public float velocity;
public float maxvelocity;
// Use this for initialization
void Start () {
	m_CharacterController = GetComponent<Rigidbody>();
	m_MoveDir.x = 0f;
	m_MoveDir.y = 0f;
	m_MoveDir.z = 0f;
	j_MoveDir.x = 0f;
	j_MoveDir.y = 0f;
	j_MoveDir.z = 0f;
	velocity = m_CharacterController.velocity.z;
}

// Update is called once per frame
void Update () {
	//m_CharacterController.velocity = new Vector3 (0f, 0f, 5f);
	velocity = m_CharacterController.velocity.z;
	if (Input.GetKey ("w")) {
		m_MoveDir.z=+1;
	}
	if (Input.GetKey ("s")) {
		m_MoveDir.z=-1;
	}
	if (Input.GetKey ("a")) {
		m_MoveDir.x=-1;
	}
	if (Input.GetKey ("d")) {
		m_MoveDir.x=+1;
	}
	if (Input.GetKey ("space")) {
		j_MoveDir.y=+1;
	}
	m_CharacterController.AddRelativeForce (m_MoveDir.normalized * Time.deltaTime * strong);
	m_MoveDir = new Vector3 (0f, 0f, 0f);
	m_CharacterController.AddRelativeForce (j_MoveDir * Time.deltaTime * strong2);
	j_MoveDir = new Vector3 (0f, 0f, 0f);
	if (velocity >= 0)
	if (velocity > maxvelocity) {
		m_CharacterController.AddRelativeForce (m_MoveDir.normalized * Time.deltaTime * -strong);
		Debug.Log ("hello");
	}
		/*if(velocity<0)
		if (Mathf.Abs(velocity) > maxvelocity)
		m_CharacterController.velocity = new Vector3 (m_CharacterController.velocity.x, m_CharacterController.velocity.y, -maxvelocity);
	//Distance() devo lavorare su distance mi sa; e uso il normalize;
	*/	
	/*if(Input.GetKeyUp("w")){
		m_CharacterController.velocity =new Vector3(0f, 0f,m_CharacterController.velocity.z/4);
	}
	if (Input.GetKey ("s")) {
		m_CharacterController.AddRelativeForce (m_MoveDir2 * Time.deltaTime * strong2);
	}
	if(Input.GetKeyUp("s")){
		m_CharacterController.velocity =new Vector3(0f, 0f,m_CharacterController.velocity.z/4);
	}*/
}

}

I think what you want to do is just set your velocity to the max velocity IF the velocity is greater than the max velocity. So, in pseudo code:

if (myVelocity > maxVelocity)
  myVelocity = maxVelocity;

Some other advice for your script: if you want to set a vector to zero, you can just do this (especially for your Start() function):

m_MoveDir = Vector3.zero;

Also, Unity has some convenient functions in their Vector3 class to help you know what direction your vector is. Instead of using “m_MoveDir.z=+1;”, you can do this:

m_MoveDir = Vector3.forward;

Hope that all helps.