- Home /
Making a simple grid
Hi ive decided to make a grid from objects ie alot of squares connected together, i would like to have objects spawned on the individual sqaures by using the mouse click these objects need to appear on the center on the square only, but at the moment they are going anywhere any ideas on a solution?
Answer by Xenox · Mar 23, 2013 at 02:11 PM
var myCam : Transform;
var maxDistance : int = 50;
var dirtBlock : Transform;
function Update(){
Screen.lockCursor = true;
var ray = new Ray(myCam.position, myCam.forward);
var hit : RaycastHit;
if(Physics.Raycast (ray, hit, maxDistance)){
if(Input.GetKeyDown(KeyCode.Mouse1) && hit.transform.gameObject.tag != "BuildingBlock"){
var hitPointX : int = hit.point.x;
var hitPointZ : int = hit.point.z;
print("Hit point: (X: " + hitPointX + ", Z: " + hitPointZ);
Instantiate(dirtBlock, Vector3(hitPointX + 0.5, 0.5, hitPointZ + 0.5), Quaternion.identity);
}
if(Input.GetKeyDown(KeyCode.Mouse0) && hit.transform.gameObject.tag != "Terrain"){
Destroy(hit.transform.gameObject);
}
}
}
Take a look at this code. By making a single terrain and an (1, 1, 1) sized block prefab, you can spawn those blocks on the invisible grid. Ofcourse you need to modify the code a little bit for it to work the exact way you want, but thats the basics of how you spawn on grid very easily.
Hope you find this usesful!
Answer by z25assassins · Apr 03, 2017 at 06:51 AM
try starting with the basics here https://www.youtube.com/watch?v=Q0nbfq-f1os
Your answer
Follow this Question
Related Questions
Spawning objects help 2 Answers
Spawn Object 1 Answer
Updating prefab information live 2 Answers
Spawn Object On Gui (button Press) 2 Answers
Random spawn of object on trigger 3 Answers