- Home /
Block puzzle (1010) help
Hello Friends,
![alt text][1]
Im developing a block puzzle game (replica of this: https://play.google.com/store/apps/details?id=com.differencetenderwhite.skirt&hl=en)
I have a grid of 8X8 cells. I have colliders (Triggers) on each cell. Also there is a collider on the shape (red) which can be dragged in the grid and can be placed inside the grid. Now how can I determine where I should snap the object (on which cells it should be placed?)
Thanks in advanced. -Monish H. Vyas [1]: /storage/temp/103098-capture.png
first of all i would not use a collider system. i would create an array of arrays in C#. this way you have 2D info to represent info about certain squares on your board. then i would use small reletive coordinates that go with the pieces for checking for a fit. to snap objects you simply use mathf.round against positions. if you need code example for such a system let me know.
@toddisarockstar Thank you for your valuable feedback. I will try this new approach and will let you know. If things don't work I'll ask your help for a code example.
float ShapeX = $$anonymous$$athf.Clamp($$anonymous$$athf.Round( 2f * transform.position.x) / 2, -GRID_BOUND, GRID_BOUND);
float ShapeY = $$anonymous$$athf.Clamp($$anonymous$$athf.Round( 2f * transform.position.y) / 2, -GRID_BOUND, GRID_BOUND);
Shadow.transform.position = new Vector2(ShapeX, ShapeY);
Shadow is a game object that represents the prospected position of the shape in the grid. The shape will be placed at Shadow.position when Input.Get$$anonymous$$ouseButtonUp(0) is called.
I am rounding the position to snap it so values (0.5, 1.0, 1.5, etc...) and clamoing the whole value to allow dropping the shape only inside the grid.
GRID_BOUND is a float constant calculated based on cell size and number of cells.
Hello guys, I seem to have solve the issue of deter$$anonymous$$ing the cell is available or not by using an array. Thanks to @toddisarockstar
Now I am using position of each cell of the shape to deter$$anonymous$$e on which cell it will be put in the grid. Now the problem is the values are in floats so I cannot use a switch statement. How can I get cell index in both X and Y directions using transform.position.
P.S. Right now Im using exact values 1.8, 1.3, etc.. But I want to create a formula to avoid using multiple condition checking.
//this will give you 4!!
$$anonymous$$athf.RoundToInt (3.7f);
//this will give you a 3//
$$anonymous$$athf.FloorToInt(3.7f);
// one of these should work to give an intreger!!
$$anonymous$$athf.RoundToInt (transform.position.x);
$$anonymous$$athf.FloorToInt(transform.position.x);
Your answer
Follow this Question
Related Questions
How to detect collision of a wall with grid movement? 1 Answer
How can i make the Unity2d grid use rounded numbers? 1 Answer
!URGENT! How to make a grid of clickable objects. 1 Answer
grid-based block placement system with touch input 1 Answer
Keyboard not working in build (is working in editor) 0 Answers