-
Notifications
You must be signed in to change notification settings - Fork 0
/
SwordStrikeScript
100 lines (82 loc) · 2.28 KB
/
SwordStrikeScript
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
98
99
100
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AttackScript : MonoBehaviour
{
public GameObject sword;
public Animator animator;
Rigidbody2D rb2d;
itemHandler iH;
CharacterController2D cc2d;
public Transform swingPoint;
float attackRange = 1;
public LayerMask enemies;
float hitDamage = 40;
float hitTime;
float strikeDuration = 0.4f;
bool canHit;
bool canDeal;
public bool striking;
void Start()
{
iH = GetComponent<itemHandler>();
rb2d = GetComponent<Rigidbody2D>();
cc2d = GetComponent<CharacterController2D>();
}
void Update()
{
checkIfCanHit();
//Når man trykker, tjekker den om man kan slå endnu
if (Input.GetKeyDown(KeyCode.Space))
{
if (canHit)
{
attack();
}
}
//Hvis angrebet er igangværende
if (striking)
{
rb2d.velocity = new Vector2(0,0);
//Halvvejs gennem svinget tager fjender skade
if(Time.time > hitTime + strikeDuration/2)
{
if (canDeal)
{
Collider2D[] Hits = Physics2D.OverlapCircleAll(swingPoint.position, attackRange, enemies);
foreach (Collider2D enemy in Hits)
{
enemy.GetComponent<enemyHealthScript>().takeDamage(hitDamage);
}
canDeal = false;
}
}
}
}
void checkIfCanHit()
{
//Hvis man har fået sværdet, står på jorden og ikke allerede er igang med at slå, kan man slå med sværdet.
if (iH.swordObtained)
{
if (Time.time > hitTime + strikeDuration)
{
canHit = true;
striking = false;
}
if (cc2d.jumpState > 0)
{
canHit = false;
striking = false;
}
}
else { canHit = false; }
}
void attack()
{
animator.SetTrigger("strike");
striking = true;
canDeal = true;
canHit = false;
hitTime = Time.time;
}
}