Creating blocks script! Need help :)

Hey guys.
im trying to make a minecraft likely game, and im totally new to this. i never did JavaScript before but now im trying :slight_smile: I made a block prefab witch is 4 cubes stick together. alt text

Okay so i tried me best to made a script witch is gonna made the cube im holding mouse over gray and when i click on it there is gonna be one more of these boxes.
But when i click there comes a new box but it randomly spawns in the air :S

can anyone tell me what im doing wrong? :slight_smile:

`var Cube : Transform;

function OnMouseOver() {

renderer.material.color = Color.gray;

if (Input.GetButtonDown(“Fire1”)) {
Instantiate(Cube, transform.position, transform.rotation);

}

}

`

I was also trying to make a little spawn cube where the other box could spawn but im not sure how to do it :slight_smile:

Well here is a script where by clicking, the object is created. This is part of the answer to your question.

var newObject : Transform;

function Update () { 
	if (Input.GetButtonDown("Fire1")) {
		Instantiate(newObject, transform.position, transform.rotation);
	}
}

As for making the spawn cube, make a cube, and make sure that it is not touching any other objects. Then name the cube “spawnCube.” On your script, write Find(“spawnCube”). Also add the spawnCube variable. Now the cube should spawn on the spawnCube. Oh, and turn off the mesh renderer on the spawn cube to make it invisible and so it doesn’t interfere with objects.

Maybe this will help (snaps cubes to a grid):

var blockPrefab : GameObject; //Your block prefab
var hit : RaycastHit; //Info about where and what the raycast hit
var range : float = 5; //Max range of the raycast
var blockLayer : LayerMask = 1; //Put your block in a layer and select that layer here (make sure its not the same as the player)

function Update () {
    if (Input.GetMouseButtonDown(1))
        PlaceBlock();
    if (Input.GetMouseButtonDown(0))
        DestroyBlock();
}

function PlaceBlock() {
    if (HitBlock()) {
  	    var cube = Instantiate(blockPrefab ,hit.transform.position + hit.normal, Quaternion.identity); 
    }
}

function DestroyBlock(){
    if(HitBlock()){
	    Destroy(hit.transform.gameObject);
    }
}

function HitBlock() : boolean{
    return Physics.Raycast(transform.position, transform.forward, hit, range, blockLayer);
}