physics for wheeled robot

Please help me, I am really stuck.
I have one wheeled robot:

and I want to write a program to controll it.

The robot is made from few parts but every one is a child of main part. Rigidbody and one script is attached to the main part. This is the script:

using UnityEngine;
using System.Collections;

public class moving : MonoBehaviour {

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {

	}
	public float velocity = 0, maxVelocity = 4, acceleration = 0.25f;
	void FixedUpdate()
	{
		if(Input.GetKey(KeyCode.UpArrow) && Input.GetKey(KeyCode.DownArrow))
		{
			if(velocity>=acceleration)velocity-=acceleration;
			else if(velocity<=-acceleration)velocity+=acceleration;
			else velocity = 0;
		}
		else if(Input.GetKey(KeyCode.UpArrow))
		{
			velocity = Mathf.Min(maxVelocity,velocity+acceleration);
		}
		else if(Input.GetKey(KeyCode.DownArrow))
		{
			velocity = Mathf.Max(-maxVelocity,velocity-acceleration);
		}
		else if(velocity != 0)
		{
			if(velocity>=acceleration)velocity-=acceleration;
			else if(velocity<=-acceleration)velocity+=acceleration;
			else velocity = 0;
		}
		Vector3 moveDirection = new Vector3(0, 0, (velocity*Time.deltaTime));
		moveDirection = transform.TransformDirection(moveDirection);

		float mag = moveDirection.magnitude;
		moveDirection -= new Vector3 (0,moveDirection.y,0);
		moveDirection.Normalize();
		moveDirection *= mag;

		Debug.Log ("Y: "+moveDirection.y+", magnitude: "+moveDirection.magnitude);
		rigidbody.MovePosition(transform.position + moveDirection);
		if(Input.GetKey(KeyCode.LeftArrow))
		{
			rigidbody.MoveRotation(Quaternion.Euler(transform.rotation.eulerAngles+new Vector3(0,-1,0)));
		}
		if(Input.GetKey(KeyCode.RightArrow))
		{
			rigidbody.MoveRotation(Quaternion.Euler(transform.rotation.eulerAngles+new Vector3(0,1,0)));
		}
	}
}

(Rotation isnt done properly yet but I have to solve this problem at first.)

So I use MoveRotation and MovePosition to move this robot. If I start to hold up arrow, the variable velocity is increased every FixedUpdate call until it reaches maxVelocity.

It works well, but only on horizontal or slighty inclined surface.

If ther is a wall (so i want robot to stop), it somethimes (very often) does this:

Note, that in the right image only the right rocket holder touched the wall.

So please help, I really need to solve it and i am stuck for a long time. I tried to manipulate with frictions. I tried to attach the script to rear or front wheels and use joints or set every part as child of this wheels. I am soo desperate. I didnt think it will be so hard to program the robot which has constan speed most of the time.

Hi lukas_balaz,

You can have a max speed with AddForce or AddTorque. You have just to use Drag and AngularDrag on the rigidbody component options. Generaly it’s zero, but you can change to positive value to make a max speed.

So in your script, you have just to call “rigidbody.AddForce(transform.forward, Forcemode.Force);” and modify the drag and angular drag property to control the robot velocity.

See here : Unity - Scripting API: Rigidbody.angularDrag

And here : Unity - Scripting API: Rigidbody.drag