using UnityEngine; using System.Collections; namespace MontanaJohns.Actors { [RequireComponent(typeof(Rigidbody2D))] public class Enemy : Actor { protected GameObject player; protected float attackRate = 0.5f; protected float nextAttackTime = 0f; protected override void Awake() { base.Awake(); player = GameObject.FindGameObjectWithTag("Player"); } // Update is called once per frame void Update() { MoveTowardsPlayer(); Attack(); CheckHealth(); } void MoveTowardsPlayer() { if (player.transform.position.x < transform.position.x) Move(-stats.speedMultiplier * 0.5f); else Move(stats.speedMultiplier * 0.5f); } void CheckHealth() { if (health <= 0) Destroy(gameObject); } void Attack() { if (Mathf.Abs(player.transform.position.x - transform.position.x) <= 3 && Time.time >= nextAttackTime) { _animator.SetTrigger("attack"); player.GetComponent().TakeDamage(1); nextAttackTime = Time.time + 1f / attackRate; } } private void OnCollisionEnter2D(Collision2D other) { if (other.gameObject.tag == "Projectile") { Debug.Log("Enemy: Hit by projectile"); TakeDamage(1); Destroy(other.gameObject); } } } }