Watch Tower Warriors

A school assignment where everyone had to make a tower defense game. For this assignment i made a very simple tower defense game with some free art assets.
With this assignment I really discoverd that I enjoy the lower level side of programming, things like behind the scenes systems are the parts where I realy shine and can do a lot of good things.


Here you see the very simple path the enemies take and wehere you can place the towers, when you select a tower a green ring appears around it this shows the range that the tower will detect enemies in




To get every enemy within the range of the tower I wrote this script that checks if an enemy collided with the collider of the range object setting it as the target,


and starts shooting until either the enemie dies or leaves the range of the tower

getting the the first enemie that enters the range


public class GetEnemy : MonoBehaviour
{
    [SerializeField] public GameObject target;

    private void OnTriggerStay2D(Collider2D collision)
    {
        if (collision == null)return;
        if (target != null)return;
        if (target == null && collision.gameObject.CompareTag("Enemy")) target = collision.gameObject;
    }

    private void OnTriggerExit2D(Collider2D collision)
    {
        if (collision.gameObject == target)target = null;
    }
}
                    

starting to shoot at that enemie


IEnumerator Shooting()
{
    while (!getEnemy.target != null) 
    {
        Instantiate(bullet, transform);
        yield return new WaitForSeconds(1f);
    }
}