- Home /
How to divide my terrain into selectable squares?
Hi.
My question is how can you divide the terrain into selectable squares where the characters are intanciados, the purpose of this is that I have to create lines or paths to attack my characters similar to the style vs Plants. Zombie, the player selects a division and this division lights up and when the player selects a character is intanciado in that division.
I have no idea how to do it, any suggestions will be helpful.
Thank you and forgive my English.
Answer by tormentoarmagedoom · Sep 15, 2017 at 07:34 AM
Good day @Abraham1532002 !
There are a lot of ways to do this.
In one of my projects, i did a simple mode. I did all objects/characters/everything, must be placed at a coords multiple of 10, (10, 20, 30, -10 , -20 ...), this way is like creating a virual grid.
Then i calculate the position of the mouse in the world space, knowing wich position.x and pozition.z multiple of 10 is closer, using this (the same with Z coord):
float floatMousePosX = Camera.main.ScreenToWorldPoint(Input.mousePosition.x);
int gridMousePosX = 10 * Mathf.RoundToInt(floatmousePosX/10);
//Divided the float by 10 and multilyed again by 10 will give you a rounded to 10 number
This way, you recieve a gridMousePos multiple of 10, (for example (10,-20) or (-230,150). Then If want to highlight or create objects in the grid, they must have a 10x10 size with center at some of this points.
If need more help on how to do this, just ask for more using @tormentoarmagedoom . I will check this question again in few hours!
If this helped you just a little, Upvote and Check the answer!
Bye :D
Ok thanks, but you will not have an idea how to prevent a GameObject from being placed on top of another, that exists as a checker that says this is earth and this is a player, using his method.
Yes you can prevent objects to be placed at same place, for example :
As all objects have/will have a exact place (10, 20 30), once something is placed, you only need to store that position in some variable, and when placing things make a loop that recalculate the random if the result is equal to some stored position.
Or, use colliders to respawn something if it spawns inside another collider
You can always create a script or go to assets store and download/buy one to create a scenario with grid.
$$anonymous$$ark the answer please! :D
Bye :D
the last $$anonymous$$i question on my part, and sorry for the inconvenience, except I never had the need to do something like this, but, now, how can I make the grid where the mouse is placed, shine or something similar, that confirms its presence to the user. Thank you
Hello @Abraham1532002 !
First, mark my answer as correct please :D
Then, you can do script that calculates the position of the mouse, and know then wich tile are you pointing
float float$$anonymous$$ousePosX = Camera.main.ScreenToWorldPoint(Input.mousePosition.x);
int grid$$anonymous$$ousePosX = 10 * $$anonymous$$athf.RoundToInt(floatmousePosX/10);
This way, grid$$anonymous$$ousePosX will be always the multiple of 10 closer to the mouse position.
Do the same for Z coord. Then in the Update() check every frame what grid$$anonymous$$ousePosX & grid$$anonymous$$ousePosZ is poiting the mouse. and you will have a realtime variable with the mouse grid position.
It's a pleasure to help! :D