- Home /
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
Answer by Klarzahs · Feb 18, 2019 at 01:22 PM
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220612191815im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Wall Running on Curved Surfaces 0 Answers
Movement from box to box 1 Answer
How to move only one object, not multiple objects 0 Answers
How to make an enemy move along a wall? 1 Answer
Wall Jump Animation 0 Answers