Simple Movment

i am new to unity,
how can i create a simple movement for an object that don’t sink to other objects while moving?

i’ve add Rigidbody and BoxCollider to them all.

i wrote this and attached them to the object

var s = 5;

function Update () {

if(Input.GetKey (“up”)){



if(Input.GetKey (“down”)){



The problem is that When the player collisions another object, it sinks into the object a little and if I hold the arrow key down, the player sinks into and comes out from the object regularly.

It leads the camera which is behind the player, to go back and forth regularly.
How do I solve this problem?

Hi Seyyed.

I suppose you’re trying to make a simple adventure game. First, check if your object has a collider (BoxCollider, MeshCollider, whatever). Then, add a RigidBody to your object (menu: Physics > Rigidbody). Then attach a script to your object who will access the Rigidbody and add forces accordingly to your needs. You may also freeze each axis rotation and/or position on the Rigidbody component (ie: you’ll want to freeze the z position if you’re trying to make a 2D adventure game).

Have you put constraint on rotation in the rigidbody component?

Try changing the variable to

var s = 5.0;

And also try changing your code to

if(Input.GetKey ("up")){
transform.Translate(Vector3(0,0,s) * Time.deltaTime);
if(Input.GetKey ("down")){
transform.Translate(Vector3(0,0,-s) * Time.deltaTime);