# Unity 3D Move Object with RigidBody

I have added RigidBody component as well as Mesh collider to an object moved with keyboard. I know that it the object will go through another (with RigidBody and Mesh collider too) if I use transform to move the object. Could you give me example of code how to move an object with RigidBody. Here is my current code (still using Transform)

if (Input.GetKey(KeyCode.RightArrow))
{
character.transform.position += Vector3.right * speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.LeftArrow))
{
character.transform.position += Vector3.left * speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.UpArrow))
{
character.transform.position += Vector3.forward * speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.DownArrow))
{
character.transform.position += Vector3.back * speed * Time.deltaTime;
}


You can use velocity for this purpose. Also you can improvise your code if you have to have inputs through arrow keys and WASD.

Rigidbody _rb;
float _speed = 30f;
void Start ()
{
_rb = character.GetComponent<Rigidbody> ();
}

void Update ()
{
float horizontal = Input.GetAxis ("Horizontal");
float vertical = Input.GetAxis ("Vertical");

Vector3 fVelocity = new Vector3 (horizontal, 0, vertical) * _speed;
_rb.velocity = fVelocity;
}


If you don't want to use WASD then use your code but change velocity instead of transform.position