-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgunBehaviour.cs
More file actions
168 lines (124 loc) · 3.68 KB
/
gunBehaviour.cs
File metadata and controls
168 lines (124 loc) · 3.68 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
using TMPro;
using UnityEngine;
public class gunBehaviour : MonoBehaviour
{
//Gun stats
[Header("Gun stats")]
public int damage;
[SerializeField] private float timeBetweenShooting, spread, spread_new, range, reloadTime, timeBetweenShots;
[SerializeField] private int magazineSize, bulletsPerTap;
[SerializeField] private bool allowButtonHold;
private int bulletsLeft, bulletsShot;
//declare enemy
[Header("Enemy")]
public GameObject enemyParent;
//bools
private bool shooting, readyToShoot, reloading;
//Reference
[Header("References")]
[SerializeField] private Camera fpsCam;
[SerializeField] private Animator gunAnimator; // animator
[SerializeField] private Transform attackPoint;
private RaycastHit rayHit;
//AUDIO
[Header("Audio")]
[SerializeField] private AudioSource shootingSound;
//UI
[Header("UI")]
public TextMeshProUGUI bullets;
public TextMeshProUGUI checkReloading;
private void Awake()
{
gunAnimator = GetComponent<Animator>();
bulletsLeft = magazineSize;
readyToShoot = true;
}
private void MyInput()
{
if (allowButtonHold)
{
shooting = Input.GetKey(KeyCode.Mouse0);
}
else
{
shooting = Input.GetKeyDown(KeyCode.Mouse0);
}
if (Input.GetKeyDown(KeyCode.R) && bulletsLeft < magazineSize && !reloading) Reload();
//Shooting
if (readyToShoot && shooting && !reloading && bulletsLeft > 0)
{
bulletsShot = bulletsPerTap;
Shoot();
}
}
public void Reload()
{
reloading = true;
Invoke("ReloadFinished", reloadTime);
}
public void ReloadFinished()
{
bulletsLeft = magazineSize;
reloading = false;
}
public void Shoot()
{
readyToShoot = false;
//Animations
gunAnimator.SetTrigger("RECOIL"); // Trigger the recoil animation
//Sound FX
shootingSound.Play();
//Spread
float x = Random.Range(-spread_new, spread_new);
float y = Random.Range(-spread_new, spread_new);
//Using the spread
Vector3 direction = fpsCam.transform.forward + new Vector3(x, y, 0);
//Raycast - shoots a ray towards the direction the camera is looking
if (Physics.Raycast(fpsCam.transform.position, direction, out rayHit, range))
{
if (rayHit.collider.CompareTag("Enemy"))
{
//raycast
rayHit.collider.GetComponent<enemyAllScript>().TakeDamage(damage);
}
else if (rayHit.collider.CompareTag("Sphere"))
{
rayHit.collider.GetComponent<resetEnemies>().RespawnEnemies(enemyParent);
}
}
bulletsLeft--;
bulletsShot--;
Invoke("ResetShot", timeBetweenShooting);
if (bulletsShot > 0 && bulletsLeft > 0)
{
Invoke("Shoot", timeBetweenShots);
}
}
private void ResetShot()
{
readyToShoot = true;
}
void Update()
{
MyInput();
//set ui text
bullets.SetText(bulletsLeft + " / " + magazineSize);
//Updates the spread if the player is moving
if (Input.GetKey(KeyCode.W) | Input.GetKey(KeyCode.S) | Input.GetKey(KeyCode.A) | Input.GetKey(KeyCode.D))
{
spread_new = spread * 1.5f;
}
else
{
spread_new = spread;
}
if (reloading)
{
checkReloading.SetText("Reloading");
}
else
{
checkReloading.SetText("");
}
}
}