- Home /
How to make the tower shoot at the one who is closer to the finish line?
How to make the tower shoot at the one who is closer to the finish line? I'm making a game in the TD genre. I have a script for the tower, when an enemy enters the area of the collider, then I check the tag and start attacking the enemy, but if the enemy has left the zone and a new one has entered, then he will attack the new one until he leaves. I probably don't explain it well, but the goal is to attack those who are closer to the finish line. The road along which the enemies are walking is an array of control points. Here is the script of the tower void OnTriggerEnter2D(Collider2D collider) { if (collider.gameObject.tag == "Zombie") { if(this.mob = null) this.mob = collider.gameObject;
} }
void OnTriggerStay2D(Collider2D collider)
{
if (collider.gameObject.tag == "Zombie")
{
if (this.mob == null) this.mob = collider.gameObject;
target = mob.GetComponent<Transform>();
transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, Mathf.Atan2(target.transform.position.y - transform.position.y, target.transform.position.x - transform.position.x) * Mathf.Rad2Deg + 90);
Fire();
}
}
void OnTriggerExit2D(Collider2D collider)
{
this.mob = null;
}
Your answer
Follow this Question
Related Questions
Instantiated object not showing in scene or hierarchy 2 Answers
How to create a Drop Down menu for a script file? 1 Answer
Can I make Money collecting script without attaching it to a object ? I tried but I got error 2 Answers
My balance in the game and my multiplier are not saving, i cant figure out why, any help? 0 Answers
Character not moving on Unity 5 2 Answers