how to move box if there is no box in front

Hello my dear

I would like to make 2d game, logical game. I need to know how to:
1 ) make move box by my player 1 step (one box size) if there is nothing after the box. If there is another box or wall you can’t move the box.

Any idea?? Please help me because I stock.

JG

Hi, sorry for the late answer.

There are multiple ways to achieve this, here are two:

  1. You can iterate over all known boxes and check whether one is in the direction behind the current box. You can get the direction by using player.transform.forward, or by substracting the player and current box position.

  2. You can RayCast from the box in the direction the player is facing, and check how far away the returned colliding object is. Note, that you’ll have to add Collider to your boxes in order for this to work.

thx I will try to do it :slight_smile: