blob: 9160e44bfe82cd3f7433f9e901cd29ce4c4eb03d (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
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) <= 5 && 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")
{
Debug.Log("Enemy: Hit by projectile");
TakeDamage(1);
Destroy(other.gameObject);
}
}
}
}
|