Placing buildings (tower defense)

I have no clue how to go about a script to place buildings.

my map is made out of cubes: 10x10

Can you provide some more info? What part do you need help with?

making them snap onto the cubes you click, etc.

Ah, this isn’t really that hard. Lets tackle it in two distinct sections. First the model, second the placement.

Now, I dont know exactly how you have your models set up, but for placements sake, lets just say that you have four walls and a top with a gun on it. (again, just for placements sake) What you will need to do is to have a 20 unit (meter) high base on it. This base needs to drop below the world center of the model. (model above Y (or Z in max) base below) This is so that you can countersink the tower into the ground and you never see the base’s bottom. Now, if you always have a flat map, we shouldn’t worry about such things, but if you have a terrain, we should.

Next, we test the placement of the tower. we measure out half the width and depth of the tower, and test the four edges. We then simply place the tower at that x and y with the z of what ever the highest point is.

var tower : Transform;
var size : Vector2=Vector2(10,10);
//place building function
function PlaceBuilding(placeAt : Vector2){
	// hit contains our original hit and servers as a test to see if we can place the object there.
	var hit : RaycastHit;
	// create out basic position
	var testPosition=Vector3(placeAt.x, 5000.0, placeAt.y);
	//test the physics and see if we can put the object there.
	if(Physics.Linecast(testPosition, testPosition-Vector3(0,5000,0),hit)){
		var placeY = -5000.0;
		// test the four edges and see where the hightest point is.
		if(Physics.Linecast(testPosition + Vector3(size.x,0,0), testPosition-Vector3(size.x,5000,0),hit))
			if(hit.point.y>placeY) placeY=hit.point.y;
		if(Physics.Linecast(testPosition + Vector3(-size.x,0,0), testPosition-Vector3(-size.x,5000,0),hit))
			if(hit.point.y>placeY) placeY=hit.point.y;
		if(Physics.Linecast(testPosition + Vector3(0,0,size.y), testPosition-Vector3(0,5000,size.y),hit))
			if(hit.point.y>placeY) placeY=hit.point.y;
		if(Physics.Linecast(testPosition + Vector3(0,0,-size.y), testPosition-Vector3(0,5000,-size.y),hit))
			if(hit.point.y>placeY) placeY=hit.point.y;
		Instantiate(tower, Vector3(placeAt.x, placeY, placeAt.z), Quaternion.identity);
	}
}

you lost me, can somebody explain a simple script that snaps the buildings onto the cube that is selected?

Assuming the game is grid-based and that height is not taken into account:

  1. Represent the level using a 2-d grid, with each cell indicating (perhaps among other things) whether or not an object can be placed there.

  2. Raycast through the mouse position (use ScreenPointToRay() to generate the ray) with the ground plane (you can use the Plane class for this - it has a ray intersection function that you can use).

  3. Use simple arithmetic to convert the intersection point to integer grid coordinates.

  4. Check the corresponding grid cell to see if it’s a valid location to place an object.

That’s about as simple as it gets, I think. If you have any problems with that, choose a specific area that you’re having trouble with, and then post back with whatever questions you have about it.

Since you actually made your map out of cubes, it is actually very simple to place towers assuming that each cube is made to hold one tower. Put a script on each cube with an OnMouseDown event. Use this event to grab the transform of the cube. The event can immediately place a tower or make a GUI call to bring up a choice of towers using the transform as a point of reference to place the tower.

I would recommend using tags on your cubes to determine if:
The cube is empty
The cube has a tower
Nothing may be built

Some of the more knowledgeable people here may cringe at the suggestion, but for a beginner, it’s probably the easiest route.

thanks! it works admirably!