Hi! I have a terrain created with Unitys terrain tools, and now I want to have a grid on the terrain. The grid will be functional. It will show where to place buildings, where AI's are going to go and other fun stuff. Now, I was wondering. How would I do this? I'm making a kind of RTS game, although not exaktly that. How do I create a grid on my terrain and if I place my mouse over a "rectangle" in the grid it should highlight. I really need help with how to achieve this, because I haven't found a good tutorial on this. Thanks!
Well first you're going to need a ray and a raycasthit I'm going to do this in c# but it's easily converted into javascript start by declaring the ray and the hit. The ray is the actual thing that will touch the object where the cursor is, and the raycasthit is what will take the shape of that hit object/point Code (csharp): Ray ray; RaycastHit hit; in the update function, you need to update the position of the ray in correspondence to the cursor. we then need to actually check where the hit is, or if an object was actually hit. You can put this in an if checking if there is a mousebuttondown or mousebutton or whatever you want Code (csharp): ray = Camera.Main.ScreenPointToRay(Input.mousePosition); //the ray is the ray, you then set the position of the hit with the out, and the 5000 is however many meters you want the ray to be valid for. you can change the value if (Physics.Raycast(ray, out hit, 5000)) { //You can attach this script to a plane w/ a single grid texture //you want to set the pos of the "grid" to where the cursor hit. but this is where it gets to the actual grid making //Lets say you want a grid that is 5 meters wide/tall. you need to use Mathf.Round to do this. //This piece of code rounds the hit position to the closest 5 meters on the x and z (the y is 0.05 so the plane is visible, but is still visibly on the ground. //Mathf.Round works by going -> Mathf.Round(valueThatNeedsToBeRounded / RoundDelta)*RoundDelta); where round delta is however much you want to round by. transform.position = new Vector3(Mathf.Round(hit.point.x/5)*5, 0.05, Mathf.Round(hit.point.z/5)*5); } what might be easier is instead of actually putting in 5 each time, you make an int that you assign to whatever you want, and then place that variable in the place of the 5s in the above code This is untested code, so there might be errors, and it might not be EXACTLY what you are looking for, but this is the basic idea. If you want a visible grid everywhere you could do two for statements and instantiate each piece of the grid every however long/tall/wide it is
I have created an asset that draws grids over terrain, and it works with or without Vectrosity. Unity Asset Store: https://www.assetstore.unity3d.com/en/#!/content/35762