- Home /
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: https://www.youtube.com/watch?v=UuvF80xb5Vc
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);
}
}
Answer by CheetahSpeedLion · Jul 13, 2017 at 01:56 AM
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.
Areleli
Answer by Cornelis-de-Jager · Jul 13, 2017 at 04:51 AM
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 = Vector3.zero;
}
void Update () {
transform.Translate (dir);
}
void MoveInDirection (Vector3 _dir) {
// Check for can move
if (!canMove)
return;
// Change Direction
dir = _dir;
// Prevent another move until collision
canMove = false;
}
OnCollisionEnter (Collision other) {
if (other.gameObject.Tag == "wall") {
// Set speed to 0
dir = Vector3.zero;
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"
Your answer

Follow this Question
Related Questions
Rigidbody Movement 1 Answer
camera move(slide) continiously on IOS 1 Answer
Detect facing normal of trigger mesh? 2 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
How to Move the Car With Touch? 1 Answer