diff options
Diffstat (limited to 'Assets/Scripts')
-rw-r--r-- | Assets/Scripts/Actors/Enemy.cs | 24 |
1 files changed, 23 insertions, 1 deletions
diff --git a/Assets/Scripts/Actors/Enemy.cs b/Assets/Scripts/Actors/Enemy.cs index 6862298..3db479f 100644 --- a/Assets/Scripts/Actors/Enemy.cs +++ b/Assets/Scripts/Actors/Enemy.cs @@ -1,11 +1,14 @@ using UnityEngine; +using System.Collections; namespace MontanaJohns.Actors { [RequireComponent(typeof(Rigidbody2D))] public class Enemy : Actor { - GameObject player; + protected GameObject player; + protected float attackRate = 0.5f; + protected float nextAttackTime = 0f; protected override void Awake() { @@ -16,12 +19,31 @@ namespace MontanaJohns.Actors // 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<Actor>().TakeDamage(1); + nextAttackTime = Time.time + 1f / attackRate; + } + } + private void OnCollisionEnter2D(Collision2D other) { if (other.gameObject.tag == "Projectile") |