Tower Defence Game Basics (work in progress)
I’ve been working on some Tower Defence game design basics for the last while, trying it in different ways to learn more about Unity Scripting etc. - this is my latest attempt. It uses a raycast from the camera to the point I’m clicking on the ‘floor’ and then it determines the colour value at the point on the texture. If it’s black then the prefab is instantiated. it does also check if the prefab ‘tower’ is too close to any other towers and only allows if it’s a certain distance away. Next step is to make the towers align to some sort of invisible grid, then I need to mix all this code with my other project I had started with creep spawns, animation and waypoints, and then I’ll obviously be using a nicer version of the texture in game, and keep the black and white stuff only for background design. So basically I will design my levels in something like photoshop or maybe inkscape.
As I said, I also have a previous 3D tower defence project work-in-progress with animated creeps etc. but I haven’t done anything on that in a while so I thought I’d start again with placing prefabs in mind. I think I’ll go 2D in the end.
Any constructive comments are very welcome
So here’s my current code:

var PrefabToUse: Transform;
var DetectionRange: float;  // Too Close To Other Towers - what radius ?
var ColliderToIgnore: String = "Floor";

function Update(){
	
if (Input.GetButtonUp ("Fire1")) {  //Check for Mouse Click
	var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
	var hit : RaycastHit;
	var hitNorm: Vector3;	

	Debug.DrawRay (ray.origin, ray.direction * 80, Color.red);
	
	if (Physics.Raycast (ray, hit)) {
		
		if (hit.transform.name == ColliderToIgnore){ //only spawn object if you're pointing at the 'Floor'
		
			hitNorm = hit.normal; //we're going to need this to make sure we align the prefab with the normal that was hit (this would probably only be needed on terrains)


			var TextureMap: Texture2D = hit.transform.renderer.material.mainTexture; //we will use this to figure out the craic
			var pixelUV = hit.textureCoord; //get the points on the texture where the raycast hit (THIS ONLY WORKS WITH MESH COLLIDERS!!!!)
	    		pixelUV.x *= TextureMap.width;
    			pixelUV.y *= TextureMap.height;
	
			if (TextureMap.GetPixel(pixelUV.x,pixelUV.y)==Color.black){ //don't even bother checking unless it's on a black space
		
				SpawnPoint = Vector3 (hit.point.x, hit.point.y+2.5, hit.point.z); //instantiate above the ground (2.5 above - which is half the height of the cube 
				//SpawnPoint = hit.point + Vector3(0,2.5,0); //instantiate above the ground (2.5 above - which is half the height of the cube 

			
				var OverlapColliders: Collider[] = Physics.OverlapSphere(SpawnPoint, DetectionRange); //create an array of overlapping colliders
				if(OverlapColliders.Length < 2){  // at the moment we just check and if it overlaps with the floor and 1 more collider, do nothing. This will have to change when enemies are moving about.
					Instantiate (PrefabToUse, SpawnPoint, Quaternion.identity);
				}

			}	
			
		}
		
	}

} // End of Check for Mouse Click

} // End of Update()

Tower Defence Game Basics (work in progress)

I’ve been working on some Tower Defence game design basics for the last while, trying it in different ways to learn more about Unity Scripting etc. - this is my latest attempt. It uses a raycast from the camera to the point I’m clicking on the ‘floor’ and then it determines the colour value at the point on the texture. If it’s black then the prefab is instantiated. it does also check if the prefab ‘tower’ is too close to any other towers and only allows if it’s a certain distance away. Next step is to make the towers align to some sort of invisible grid, then I need to mix all this code with my other project I had started with creep spawns, animation and waypoints, and then I’ll obviously be using a nicer version of the texture in game, and keep the black and white stuff only for background design. So basically I will design my levels in something like photoshop or maybe inkscape.

As I said, I also have a previous 3D tower defence project work-in-progress with animated creeps etc. but I haven’t done anything on that in a while so I thought I’d start again with placing prefabs in mind. I think I’ll go 2D in the end.

Any constructive comments are very welcome

So here’s my current code:


var PrefabToUse: Transform;
var DetectionRange: float;  // Too Close To Other Towers - what radius ?
var ColliderToIgnore: String = "Floor";

function Update(){
	
if (Input.GetButtonUp ("Fire1")) {  //Check for Mouse Click
	var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
	var hit : RaycastHit;
	var hitNorm: Vector3;	

	Debug.DrawRay (ray.origin, ray.direction * 80, Color.red);
	
	if (Physics.Raycast (ray, hit)) {
		
		if (hit.transform.name == ColliderToIgnore){ //only spawn object if you're pointing at the 'Floor'
		
			hitNorm = hit.normal; //we're going to need this to make sure we align the prefab with the normal that was hit (this would probably only be needed on terrains)


			var TextureMap: Texture2D = hit.transform.renderer.material.mainTexture; //we will use this to figure out the craic
			var pixelUV = hit.textureCoord; //get the points on the texture where the raycast hit (THIS ONLY WORKS WITH MESH COLLIDERS!!!!)
	    		pixelUV.x *= TextureMap.width;
    			pixelUV.y *= TextureMap.height;
	
			if (TextureMap.GetPixel(pixelUV.x,pixelUV.y)==Color.black){ //don't even bother checking unless it's on a black space
		
				SpawnPoint = Vector3 (hit.point.x, hit.point.y+2.5, hit.point.z); //instantiate above the ground (2.5 above - which is half the height of the cube 
				//SpawnPoint = hit.point + Vector3(0,2.5,0); //instantiate above the ground (2.5 above - which is half the height of the cube 

			
				var OverlapColliders: Collider[] = Physics.OverlapSphere(SpawnPoint, DetectionRange); //create an array of overlapping colliders
				if(OverlapColliders.Length < 2){  // at the moment we just check and if it overlaps with the floor and 1 more collider, do nothing. This will have to change when enemies are moving about.
					Instantiate (PrefabToUse, SpawnPoint, Quaternion.identity);
				}

			}	
			
		}
		
	}

} // End of Check for Mouse Click

} // End of Update()

  1. pojomcbooty posted this
Blog comments powered by Disqus