Logo

Enemy.cs (Tower Defense)

Expire never
Posted on 13/05/2023
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Enemy : MonoBehaviour
{
    private Rigidbody2D rb;
    public Animation explode;
    // Start is called before the first frame update
    void Awake()
    {
        rb = GetComponent<Rigidbody2D>();   
    }

    // Update is called once per frame
    void Update()
    {
        transform.Rotate(Vector3.forward * 5f);
    }

    public void AssignTarget(Base target, float speed) 
    { 
        rb.AddForce((target.transform.position - transform.position).normalized * speed,
                    ForceMode2D.Impulse);
    }
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.CompareTag("Base"))
        {
            collision.gameObject.GetComponent<Base>().Damage(1);
            Debug.Log("Damaged");
        }

        if (!collision.gameObject.CompareTag("Enemy"))
        {
            Debug.Log("Enemy collided");
            Destroy(this.gameObject);
        }
    }
}
Clone Code
Twitter iconFacebook iconLinkedin icon