-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayerCombat.cs
58 lines (51 loc) · 1.49 KB
/
PlayerCombat.cs
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerCombat : MonoBehaviour
{
public Animator animator;
public Transform attackPoint;
public LayerMask enemyLayers;
public float attackRange = 0.5f;
public int attackDamage = 35;
public float attackRate = 2f;
float nextAttackTime = 0f;
static public int playerScore = 0;
void Update()
{
if (Time.time >= nextAttackTime) {
if (Input.GetKeyDown(KeyCode.J)) {
Attack();
nextAttackTime = Time.time + 1f / attackRate;
}
}
if (Input.GetKeyDown(KeyCode.Y)) {
Debug.Log(playerScore);
}
}
void Attack()
{
// Animation
animator.SetTrigger("Attack");
// Enemy Detection
Collider2D[] hitEnemies = Physics2D.OverlapCircleAll(attackPoint.position, attackRange, enemyLayers);
if (hitEnemies.Length > 0) {
FindObjectOfType<AudioManager>().Play("Sword Hit");
}
else {
FindObjectOfType<AudioManager>().Play("Sword Swing");
}
// Damage
foreach(Collider2D enemy in hitEnemies) {
enemy.GetComponent<Enemy>().TakeDamage(attackDamage);
}
}
private void OnDrawGizmosSelected()
{
Gizmos.DrawWireSphere(attackPoint.position, attackRange);
}
public void DamageAnim()
{
animator.SetTrigger("Damaged");
}
}