using UnityEngine; using System.Collections; /// /// Includes both collision attack and bullet firing /// public class EnemyAttackScript : MonoBehaviour { GameObject player; public float Damage = 20; float timer = .5f; float counter; bool playerInRange; void Start() { counter = 0; player = GameObject.FindGameObjectWithTag("Player"); } void Update() { counter += Time.deltaTime; if (counter >= timer && playerInRange) { counter = 0f; player.GetComponent().TakeDamage(Damage); } } void OnTriggerEnter2D(Collider2D other) { var player = other.gameObject.GetComponentInParent(); if (player != null) { playerInRange = true; } } void OnTriggerExit2D(Collider2D other) { var player = other.gameObject.GetComponentInParent(); if (player != null) { playerInRange = false; } } }