-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmeleeEnemy.cs
More file actions
97 lines (78 loc) · 2.59 KB
/
meleeEnemy.cs
File metadata and controls
97 lines (78 loc) · 2.59 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class meleeEnemy : MonoBehaviour
{
[SerializeField] private float attackCooldown;
[SerializeField] private float range;
[SerializeField] private float colliderDistance;
[SerializeField] private int damage;
[SerializeField] private BoxCollider2D boxCollider;
[SerializeField] private LayerMask playerLayer;
private int shilddamage;
private float coolDownTimer;
Animator anim;
private MainPlayerMovement playerhealth;
bool isdead;
bool shild;
public Transform player;
PlayerShieldInSight enemyinSight;
private void Awake()
{
anim = GetComponent<Animator>();
enemyinSight = GameObject.Find("EnemyDetector").GetComponent<PlayerShieldInSight>();
}
private void Update()
{
coolDownTimer += Time.deltaTime;
if (PlayerInSight() && (playerhealth.currentHealth > 0))
{
if (coolDownTimer >= attackCooldown)
{
coolDownTimer = 0;
anim.SetTrigger("meleeAttack");
}
}
if (Input.GetKey(KeyCode.Mouse1))
{
shild = true;
}
else
{
shild = false;
}
if (enemyinSight.EnemyINsight() && shild == true)
{
shilddamage = 0;
damage = shilddamage;
}
else if (shild == false || !enemyinSight.EnemyINsight())
{
shilddamage = 10;
damage = shilddamage;
}
}
bool PlayerInSight()
{
RaycastHit2D hit = Physics2D.BoxCast(boxCollider.bounds.center + transform.right * range * transform.localScale.x * colliderDistance,
new Vector3(boxCollider.bounds.size.x * range , boxCollider.bounds.size.y, boxCollider.bounds.size.z),0,Vector2.left,0, playerLayer );
if (hit.collider != null)
{
playerhealth = hit.transform.GetComponent<MainPlayerMovement>();
}
return hit.collider != null;
}
private void OnDrawGizmos()
{
Gizmos.color = Color.red;
Gizmos.DrawWireCube(boxCollider.bounds.center + transform.right * range * transform.localScale.x * colliderDistance,
new Vector3(boxCollider.bounds.size.x * range, boxCollider.bounds.size.y, boxCollider.bounds.size.z));
}
public void PlayerDamage()
{
if (PlayerInSight() && (playerhealth.currentHealth > 0))
{
playerhealth.TakeDamage(damage);
}
}
}