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.