Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

2d puzzle game

How to move the player to the last sprite or grid, in the direction of button pressed, leaving the travelled sprite as colliders and also change color or something to show so the player wont be able to travel through it again. As a result it will create a path. Only if the player move backwards the sprite or grid will again come to normal .

i hope you will understand my problem.


Comments

  • You can have a 2D array with positions where the player can and can not move.

    You can use Input.GetButton or Input.GetAxis() to know in which direction to move.

    Finally, check if there is an input and if there is, loop through the width or height depending on input being horizontal or vertical

    next, check if the player's current position + the position you want to move in is free and if it is free, move your player's position by 1 tile in the position you want to move in.

    last, change the position on the positions array to be not available


    This code may help but you will have to implement the drawing of the character based on Player_x and Player_y


    //Default value of an int = 0

    var Positions = new int[4, 4];

    var Player_x = 0;

    var Player_y = 0;

    void Update()

    {

    Vector2 input = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));

    if (input != new Vector2())

    {

    var temp = Positions.GetLength(0);

    if (input.y != 0)

    {

    temp = Positions.GetLength(1);

    }

    for (int i = 0;i < temp; i++)

    {

    if (Positions[player_x + input.x, player_y + input.y] == 0)

    {

    player += input;

    Positions[player_x + input.x, player_y + input.y] = 1;

    }

    else

    {

    break;

    {

    }

    }

    }

Sign In or Register to comment.