- Home /
Click to move in a 3d-blockbased single mesh
Hello there, this is going to be my first question although I've been reading Unity answers for a while now.
I'm trying to make a 3d game with block-based terrain just for learning purposes. I've read a lot about optimizing the terrain combining groups of cubes/blocks into one single mesh. I've made a script to combine each cube/block mesh into one single mesh like here:
I'm using a simple script to move the player (capsule) 1 unit when left, right, up or down is pressed and it's working perfectly fine for my goal.
The thing is, since that terrain is now a single mesh, I don't know if it's possible to handle each cube/block on its own so that I can click on one of them and move until there with grid-based movement like I'm doing with left, right, up and down buttons.
To sum up, I want to move with a grid-based movement system to the block I've clicked inside the single mesh. I'll explain it below.
My first question is how to move to the yellow block?. I think it's more or less easy since maybe I can calculate the difference in X and use Move.Towards() with fixed units i.e 3 units in this case.
But, my second question is, what if there are obstacles in between? I want to move to the green block. I have to use some pathfinding algorithm but.. how? Since there is only a single mesh, how can I calculate the best path to a block if I don't have the block coords stored anywhere? I don't know that the purple obstacles are there and that I have to avoid them. How can I do it?
Thanks so much in advance!
Answer by oroora6_unity · Apr 10, 2018 at 11:36 PM
Pathfinding isn't something easy, all the tips i can give you for now is to look into Graphs (which is how our very much loved lists works!) You can learn about graph theory here and then apply it maybe with an array of nodes collegated by edges by giving each node a variable containing a position. once you've done that try going for a map generation, so after the graph is ready build a map according to it. If you reach that point you should move towards creating a pawn to move in your game through a Dijkstra algorithm and only then once you have complete knowledge about what pathfinding is, you should move towards optimization by making the static blocks turn into one singular mesh and moving towards an A* pathfinding algorithm which is more efficent, but harder to apply. I remember doing something like this in the past, if you put your soul into learning this, you should be fine and make your first pathfinding strategic game!
Thanks for the answer @oroora6_unity ! So, if I've understood it properly, what I really need is to store the blocks/cubes/tiles position in some kind of array or whatever so I can instantiate the map from there and then use a pathfinding algorithm since I know each "pathable" position? But what If I want to do the map manually? I mean, I want to create the map block by block and then merge them into one single mesh? I still need to store the position data somewhere before merging? Is it really necessary to have the position stored to achieve this?
Your answer
Follow this Question
Related Questions
store the user touch input 0 Answers
How to detect what tile player is standing on in a tile-based game 2 Answers
3D Grid Based Movement (X-Com 2012 etc) – How to implement vertical movement? 0 Answers
2D Isometric grid movement direction 0 Answers
Save a precalculated grid path into a jagged 2d array ready to be loaded when need 0 Answers