blob: 80fa46f40185a222c1320b39aa138c04ed9b9c81 (
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
|
using UnityEngine;
using System.Collections;
/// <summary>
/// Includes both collision attack and bullet firing
/// </summary>
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<PlayerHealthScript>().TakeDamage(Damage);
}
}
void OnTriggerEnter2D(Collider2D other)
{
var player = other.gameObject.GetComponentInParent<PlayerHealthScript>();
if (player != null)
{
playerInRange = true;
}
}
void OnTriggerExit2D(Collider2D other)
{
var player = other.gameObject.GetComponentInParent<PlayerHealthScript>();
if (player != null)
{
playerInRange = false;
}
}
}
|