I created a square that slides on a flat ground.
public class PlayerMoveScript : MonoBehaviour
{
float move = 0;
void Start (){}
void FixedUpdate ()
{
if (move == 0) {
move = Input.GetAxis ("Horizontal");
if (move != 0) {
move = 15*Mathf.Sign(move);
rigidbody2D.velocity += new Vector2 (move, rigidbody2D.velocity.y);
}
} else
{
rigidbody2D.velocity = new Vector2 (move, rigidbody2D.velocity.y);
}
}
}
On both sides of the ground lies a wall, when the square hits a wall and stops his movement, the square instead continues to push in his direction. What I trying to do, is that when the square hits the wall, the square stop the movement and stop to push on the wall, in a way that the player can choose a new direction until the square will hit a new wall.
So imagine a big picture frame, a little square lies on the center.
- The player hit the down key
- The square starts to move down, with a constant velocity
- The square hits the bottom side of the frame, and it stops
- The player choose a new direction.
- The square starts to move in the new direction
- Back to 2
N.B: The walls are rigid bodies 2d with a box collider 2d.