Looking for some tips.

I am starting a pet project outside of school (Visual and Game Programming) and I am making a TD game. I am starting with just getting the basic components out of the way in the form of prefabs before I go and do any modeling or design aspects. I was working on the AI script I would be putting on the towers and was looking for some tips on how to get around a problem ive been facing. I have the tower getting a range as a inspector varible, turning towards the enemy I have also dragged into the Inspector and Instantiating a projectile. I then have a seperate script I apply to the bullet prefab that is being spawned to move towards the target and than destory both the target and the projectile on trigger enter.

-The problems I am having is currently the way I have it set up there is only 1 type of target aquired by the tower, even if there is multiple of the same prefab it will only look at one and if all the others are in range and the paticular one it is locked on too isnt, it will not fire.
-Another issue I am having is getting the projectile and the tower to aim at the same target, I currnely have the bulletmove finding the target and using lerp to close distance move towards it, however this is random target and not nesisarly the same on that the tower is looking or generating the projectile for.

Here is the code for the Tower.

var Target :  Transform;
var Range : int;
var Projectile : GameObject;
var Speed : double;



while(true)
{
                 //Check distance from Target, Checking if target is in range
	var dist = Vector3.Distance(Target.position, transform.position);
	if (dist < Range)
	{   
                                  //face target and create projectile
		transform.LookAt(Target.position);
		var Projectile_1 = Instantiate (Projectile, transform.position, transform.rotation);
	}
                 //wait for the reload time before creating another projectile
	yield new WaitForSeconds(Speed);
}

And here is the code for the bullet Move, I would like to make the speed of bullet a global variable but have not looked into that yet, I am assuming it will be easy but who knows.

var Target;

function Update()

{
                 //search for all gameobjects with name target
	Target = GameObject.Find("Target");
                 //Move projectile towards location of Target found above
	transform.position = Vector3.Lerp(transform.position, Target.transform.position, Time.deltaTime * 5);
}

And this is the code for the Destory, I have have 1 of each of these with the others name in checking name, I will only post 1. I feel like there is a better way to get these to destroy in a single line of code but could not get it working.

function OnTriggerEnter(theCollision : Collider) 
{
		var Other = theCollision.gameObject.name;
		if(theCollision.gameObject.name == "Target")
		{
			Destroy(gameObject);
		}

}

Thank you for any help in advance, Im not looking for any free code handouts just looking for some tips as to how to solve these problems. Thank you.

OK, tower defense is pretty common around there (evidently) though they don’t share much code. I will endevor to help you in to the right direction though.

Tackle your problems in sections.

First, you have a bullet that lerps to the target. (not very believable) I would suggest using physics to propel the bullet at a speed. It could have an arc, and hit something. When it hits, it creates an explosion. So your OnTriggerEnter would become an OnCollisionEnter. Look that up, there are parameters for it. Once you have collided with something, then it explodes, this triggers the Destroy of the bullet, but not the Destroy of the other object. Next, we want to do an OverlapSphere (look it up) and get all objects within a distance to the tower.

With this, we have two basic things to check. First: What did we hit? that takes full damage. Next what else was in the blast range? Those take damage based on the distance to the center of the explosion. This is simple math that ranges from a minimum damage to a maximum damage. (remember to omit the initial collider)

OK, we have what we are going to damage, and how much, now we do this:

otherCollider.gameObject.SendMessage("doDamage", damageAmount, SendMessageOptions.DontRequireReceiver);

So basically, we tell each object we hit that we are doing damage to it. So EVERY object that could take damage has a script like this:

var hitpoints = 100;
function doDamage(amount : int){
	hitpoints -= amount;
}

Lastly, and you may not want to do this. You can add explosive force to the colliders.

Next problem, you need a script which handles the enemies and targets.

I would create a sphere collider on the base object of your turret. tell it that it is a trigger. Then what you will need to do is handle a List. Each object that enters the trigger is added to the list, each object that exits is removed from the list. Each frame, you check the current enemy against the list, if he is not on it, pick the next closest target. Also, every frame, we need to figure out if the objects on the list are still in the game, and are still alive. (remember to use a List, not an array)

Closest is relative. If you have one target it’s obvious which target to follow, however, if you have 10, closest may not always be the best. You can use Vector3.Dot to figure out which of the objects in your list is closest to where you are pointing. The highest value is the one closest to where we are looking.

I am sure there is more, but it’s almost quitting time here. :smile:

Thank You for your help I will try and go see if I can make most of that work. Seems like a bit of syntax ill have to look up but doesnt sound overly difficult. Thanks again.