How to make a cube slide and can't change direction?

I’m trying to do a movement based on Pokemon Ice Movement / Slider android game.

THIS VIDEO SHOWS THE MOVEMENT: Android Slider Game App - YouTube

The move consists on when you slide to one axis, the player needs to wait to collide with the wall to have permition to do the next move.

I’m a software engineer student so I’m familiar with programming, but I’m new on Unity and game programming so I need a bit of help right now.

I already have this code:

 public class PlayerController1 : MonoBehaviour {

 public float MovementSpeed = 20f;

 private bool prevStatusVertical;
 private bool prevStatusHorizontal;
 private bool moveHorizontal;

 private CharacterController cc;
 void Start () {
     cc = GetComponent<CharacterController> ();
 void Update () {
     float forwardSpeed = Input.GetAxis("Vertical") * MovementSpeed;
     float sideSpeed = Input.GetAxis("Horizontal") * MovementSpeed;
     //This variables will be used to check whether player is pressing keys assigned to Horizontal and Vertical
     bool statusHorizontal = Input.GetButton("Horizontal");
     bool statusVertical = Input.GetButton("Vertical");

     if (statusHorizontal && !prevStatusHorizontal) 
         moveHorizontal = true;
     if (statusVertical && !prevStatusVertical || !statusHorizontal)
         moveHorizontal = false;
     //By default character is not moving
     Vector3 speed = new Vector3 (0, 0, 0);
     if (statusVertical && !moveHorizontal)
         speed.z += forwardSpeed;
     else if (statusHorizontal)
         speed.x += sideSpeed;

     prevStatusVertical = statusVertical;
     prevStatusHorizontal = statusHorizontal;
     cc.SimpleMove (speed);


I would recommend using collision detection and a “touchingWall” boolean whenever the object is in contact with the wall. Then you can do an if( touchingWall ) and put your existing input code inside that.


If you want it to move in one direction until it collides then you con use something like the following. It will work best if there is NO RigidBody component attached:

Vector3 dir;
bool canMove = true;

void Start () {
   // initialise as zero vector
   dir =;

void Update () { 
   transform.Translate (dir);

void MoveInDirection (Vector3 _dir) {
    // Check for can move
    if (!canMove)
   // Change Direction
    dir = _dir;

   // Prevent another move until collision
   canMove = false;

OnCollisionEnter (Collision other) {
    if (other.gameObject.Tag == "wall")  {
     // Set speed to 0
     dir =;
     canMove = true;

All you need to do then is call the MoveInDirection function each time you want to move. The rest will be handled automatically. Your walls will require the Tag “wall”