HandgunScriptLPFP.cs
15.5 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
// ----- Low Poly FPS Pack Free Version -----
public class HandgunScriptLPFP : MonoBehaviour {
//Animator component attached to weapon
Animator anim;
//Main gun camera
public Camera ARCamera;
[Header("Gun Camera Options")]
//How fast the camera field of view changes when aiming
[Tooltip("How fast the camera field of view changes when aiming.")]
public float fovSpeed = 15.0f;
//Default camera field of view
[Tooltip("Default value for camera field of view (40 is recommended).")]
public float defaultFov = 40.0f;
public float aimFov = 15.0f;
[Header("UI Weapon Name")]
[Tooltip("Name of the current weapon, shown in the game UI.")]
public string weaponName;
private string storedWeaponName;
[Header("Weapon Sway")]
//Enables weapon sway
[Tooltip("Toggle weapon sway.")]
public bool weaponSway;
public float swayAmount = 0.02f;
public float maxSwayAmount = 0.06f;
public float swaySmoothValue = 4.0f;
private Vector3 initialSwayPosition;
[Header("Weapon Settings")]
public float sliderBackTimer = 1.58f;
private bool hasStartedSliderBack;
//Eanbles auto reloading when out of ammo
[Tooltip("Enables auto reloading when out of ammo.")]
public bool autoReload;
//Delay between shooting last bullet and reloading
public float autoReloadDelay;
//Check if reloading
private bool isReloading;
//Holstering weapon
private bool hasBeenHolstered = false;
//If weapon is holstered
private bool holstered;
//Check if running
private bool isRunning;
//Check if aiming
private bool isAiming;
//Check if walking
private bool isWalking;
//Check if inspecting weapon
private bool isInspecting;
//How much ammo is currently left
private int currentAmmo;
//Totalt amount of ammo
[Tooltip("How much ammo the weapon should have.")]
public int ammo;
//Check if out of ammo
private bool outOfAmmo;
[Header("Bullet Settings")]
//Bullet
[Tooltip("How much force is applied to the bullet when shooting.")]
public float bulletForce = 400;
[Tooltip("How long after reloading that the bullet model becomes visible " +
"again, only used for out of ammo reload aniamtions.")]
public float showBulletInMagDelay = 0.6f;
[Tooltip("The bullet model inside the mag, not used for all weapons.")]
public SkinnedMeshRenderer bulletInMagRenderer;
[Header("Grenade Settings")]
public float grenadeSpawnDelay = 0.35f;
[Header("Muzzleflash Settings")]
public bool randomMuzzleflash = false;
//min should always bee 1
private int minRandomValue = 1;
[Range(2, 25)]
public int maxRandomValue = 5;
private int randomMuzzleflashValue;
public bool enableMuzzleflash = true;
public ParticleSystem muzzleParticles;
public bool enableSparks = true;
public ParticleSystem sparkParticles;
public int minSparkEmission = 1;
public int maxSparkEmission = 7;
[Header("Muzzleflash Light Settings")]
public Light muzzleflashLight;
public float lightDuration = 0.02f;
[Header("Audio Source")]
//Main audio source
public AudioSource mainAudioSource;
//Audio source used for shoot sound
public AudioSource shootAudioSource;
[Header("UI Components")]
public Text timescaleText;
public Text currentWeaponText;
public Text currentAmmoText;
public Text totalAmmoText;
[System.Serializable]
public class prefabs
{
[Header("Prefabs")]
public Transform bulletPrefab;
public Transform casingPrefab;
public Transform grenadePrefab;
}
public prefabs Prefabs;
[System.Serializable]
public class spawnpoints
{
[Header("Spawnpoints")]
//Array holding casing spawn points
//Casing spawn point array
public Transform casingSpawnPoint;
//Bullet prefab spawn from this point
public Transform bulletSpawnPoint;
//Grenade prefab spawn from this point
public Transform grenadeSpawnPoint;
}
public spawnpoints Spawnpoints;
[System.Serializable]
public class soundClips
{
public AudioClip shootSound;
public AudioClip takeOutSound;
public AudioClip holsterSound;
public AudioClip reloadSoundOutOfAmmo;
public AudioClip reloadSoundAmmoLeft;
public AudioClip aimSound;
}
public soundClips SoundClips;
private bool soundHasPlayed = false;
private void Awake ()
{
//Set the animator component
anim = GetComponent<Animator>();
//Set current ammo to total ammo value
currentAmmo = ammo;
muzzleflashLight.enabled = false;
}
private void Start () {
ARCamera = GameObject.Find("AR Camera").GetComponent<Camera>();
//Save the weapon name
storedWeaponName = weaponName;
//Get weapon name from string to text
currentWeaponText.text = weaponName;
//Set total ammo text from total ammo int
totalAmmoText.text = ammo.ToString();
//Weapon sway
initialSwayPosition = transform.localPosition;
//Set the shoot sound to audio source
shootAudioSource.clip = SoundClips.shootSound;
}
//private void LateUpdate()
//{
// //Weapon sway
// if (weaponSway == true)
// {
// float movementX = -Input.GetAxis("Mouse X") * swayAmount;
// float movementY = -Input.GetAxis("Mouse Y") * swayAmount;
// //Clamp movement to min and max values
// movementX = Mathf.Clamp
// (movementX, -maxSwayAmount, maxSwayAmount);
// movementY = Mathf.Clamp
// (movementY, -maxSwayAmount, maxSwayAmount);
// //Lerp local pos
// Vector3 finalSwayPosition = new Vector3
// (movementX, movementY, 0);
// transform.localPosition = Vector3.Lerp
// (transform.localPosition, finalSwayPosition +
// initialSwayPosition, Time.deltaTime * swaySmoothValue);
// }
//}
private void Update () {
//Aiming
//Toggle camera FOV when right click is held down
/*
*
* if(Input.GetButton("Fire2") && !isReloading && !isRunning && !isInspecting)
{
ARCamera.fieldOfView = Mathf.Lerp (ARCamera.fieldOfView,
aimFov, fovSpeed * Time.deltaTime);
isAiming = true;
anim.SetBool ("Aim", true);
if (!soundHasPlayed)
{
mainAudioSource.clip = SoundClips.aimSound;
mainAudioSource.Play ();
soundHasPlayed = true;
}
}
else
{
//When right click is released
ARCamera.fieldOfView = Mathf.Lerp(ARCamera.fieldOfView,
defaultFov,fovSpeed * Time.deltaTime);
isAiming = false;
anim.SetBool ("Aim", false);
}
//Aiming end
*/
//If randomize muzzleflash is true, genereate random int values
if (randomMuzzleflash == true) {
randomMuzzleflashValue = Random.Range (minRandomValue, maxRandomValue);
}
//Timescale settings
//Change timescale to normal when 1 key is pressed
if (Input.GetKeyDown (KeyCode.Alpha1))
{
Time.timeScale = 1.0f;
timescaleText.text = "1.0";
}
//Change timescale to 50% when 2 key is pressed
if (Input.GetKeyDown (KeyCode.Alpha2))
{
Time.timeScale = 0.5f;
timescaleText.text = "0.5";
}
//Change timescale to 25% when 3 key is pressed
if (Input.GetKeyDown (KeyCode.Alpha3))
{
Time.timeScale = 0.25f;
timescaleText.text = "0.25";
}
//Change timescale to 10% when 4 key is pressed
if (Input.GetKeyDown (KeyCode.Alpha4))
{
Time.timeScale = 0.1f;
timescaleText.text = "0.1";
}
//Pause game when 5 key is pressed
if (Input.GetKeyDown (KeyCode.Alpha5))
{
Time.timeScale = 0.0f;
timescaleText.text = "0.0";
}
//Set current ammo text from ammo int
currentAmmoText.text = currentAmmo.ToString ();
//Continosuly check which animation
//is currently playing
AnimationCheck ();
//Play knife attack 1 animation when Q key is pressed
if (Input.GetKeyDown (KeyCode.Q) && !isInspecting)
{
anim.Play ("Knife Attack 1", 0, 0f);
}
//Play knife attack 2 animation when F key is pressed
if (Input.GetKeyDown (KeyCode.F) && !isInspecting)
{
anim.Play ("Knife Attack 2", 0, 0f);
}
//Throw grenade when pressing G key
if (Input.GetKeyDown (KeyCode.G) && !isInspecting)
{
StartCoroutine (GrenadeSpawnDelay ());
//Play grenade throw animation
anim.Play("GrenadeThrow", 0, 0.0f);
}
//If out of ammo
if (currentAmmo == 0)
{
//Show out of ammo text
currentWeaponText.text = "OUT OF AMMO";
//Toggle bool
outOfAmmo = true;
//Auto reload if true
if (autoReload == true && !isReloading)
{
StartCoroutine (AutoReload ());
}
//Set slider back
anim.SetBool ("Out Of Ammo Slider", true);
//Increase layer weight for blending to slider back pose
anim.SetLayerWeight (1, 1.0f);
}
else
{
//When ammo is full, show weapon name again
currentWeaponText.text = storedWeaponName.ToString ();
//Toggle bool
outOfAmmo = false;
//anim.SetBool ("Out Of Ammo", false);
anim.SetLayerWeight (1, 0.0f);
}
//Shooting
if (Input.GetMouseButtonDown (0) && !outOfAmmo && !isReloading && !isInspecting && !isRunning)
{
anim.Play ("Fire", 0, 0f);
muzzleParticles.Emit (1);
//Remove 1 bullet from ammo
currentAmmo -= 1;
shootAudioSource.clip = SoundClips.shootSound;
shootAudioSource.Play ();
//Light flash start
StartCoroutine(MuzzleFlashLight());
if (!isAiming) //if not aiming
{
anim.Play ("Fire", 0, 0f);
muzzleParticles.Emit (1);
if (enableSparks == true)
{
//Emit random amount of spark particles
sparkParticles.Emit (Random.Range (1, 6));
}
}
//Spawn bullet at bullet spawnpoint
var bullet = (Transform)Instantiate (
Prefabs.bulletPrefab,
Spawnpoints.bulletSpawnPoint.transform.position,
Spawnpoints.bulletSpawnPoint.transform.rotation);
//Add velocity to the bullet
bullet.GetComponent<Rigidbody>().velocity =
bullet.transform.forward * bulletForce;
//Spawn casing prefab at spawnpoint
Instantiate (Prefabs.casingPrefab,
Spawnpoints.casingSpawnPoint.transform.position,
Spawnpoints.casingSpawnPoint.transform.rotation);
}
/*
//Inspect weapon when pressing T key
if (Input.GetKeyDown (KeyCode.T))
{
anim.SetTrigger ("Inspect");
}
//Toggle weapon holster when pressing E key
if (Input.GetKeyDown (KeyCode.E) && !hasBeenHolstered)
{
holstered = true;
mainAudioSource.clip = SoundClips.holsterSound;
mainAudioSource.Play();
hasBeenHolstered = true;
}
else if (Input.GetKeyDown (KeyCode.E) && hasBeenHolstered)
{
holstered = false;
mainAudioSource.clip = SoundClips.takeOutSound;
mainAudioSource.Play ();
hasBeenHolstered = false;
}
*/
//Holster anim toggle
if (holstered == true)
{
anim.SetBool ("Holster", true);
}
else
{
anim.SetBool ("Holster", false);
}
/*
*
//Reload
if (Input.GetKeyDown (KeyCode.R) && !isReloading && !isInspecting)
{
//Reload
Reload ();
if (!hasStartedSliderBack)
{
hasStartedSliderBack = true;
StartCoroutine (HandgunSliderBackDelay());
}
}
//Walking when pressing down WASD keys
if (Input.GetKey (KeyCode.W) && !isRunning ||
Input.GetKey (KeyCode.A) && !isRunning ||
Input.GetKey (KeyCode.S) && !isRunning ||
Input.GetKey (KeyCode.D) && !isRunning)
{
anim.SetBool ("Walk", true);
} else {
anim.SetBool ("Walk", false);
}
//Running when pressing down W and Left Shift key
if ((Input.GetKey (KeyCode.W) && Input.GetKey (KeyCode.LeftShift)))
{
isRunning = true;
} else {
isRunning = false;
}
//Run anim toggle
if (isRunning == true) {
anim.SetBool ("Run", true);
} else {
anim.SetBool ("Run", false);
}*/
}
private IEnumerator HandgunSliderBackDelay () {
//Wait set amount of time
yield return new WaitForSeconds (sliderBackTimer);
//Set slider back
anim.SetBool ("Out Of Ammo Slider", false);
//Increase layer weight for blending to slider back pose
anim.SetLayerWeight (1, 0.0f);
hasStartedSliderBack = false;
}
private IEnumerator GrenadeSpawnDelay () {
//Wait for set amount of time before spawning grenade
yield return new WaitForSeconds (grenadeSpawnDelay);
//Spawn grenade prefab at spawnpoint
Instantiate(Prefabs.grenadePrefab,
Spawnpoints.grenadeSpawnPoint.transform.position,
Spawnpoints.grenadeSpawnPoint.transform.rotation);
}
private IEnumerator AutoReload () {
if (!hasStartedSliderBack)
{
hasStartedSliderBack = true;
StartCoroutine (HandgunSliderBackDelay());
}
//Wait for set amount of time
yield return new WaitForSeconds (autoReloadDelay);
if (outOfAmmo == true) {
//Play diff anim if out of ammo
anim.Play ("Reload Out Of Ammo", 0, 0f);
mainAudioSource.clip = SoundClips.reloadSoundOutOfAmmo;
mainAudioSource.Play ();
//If out of ammo, hide the bullet renderer in the mag
//Do not show if bullet renderer is not assigned in inspector
if (bulletInMagRenderer != null)
{
bulletInMagRenderer.GetComponent
<SkinnedMeshRenderer> ().enabled = false;
//Start show bullet delay
StartCoroutine (ShowBulletInMag ());
}
}
//Restore ammo when reloading
currentAmmo = ammo;
outOfAmmo = false;
}
/*
//Reload
private void Reload () {
if (outOfAmmo == true)
{
//Play diff anim if out of ammo
anim.Play ("Reload Out Of Ammo", 0, 0f);
mainAudioSource.clip = SoundClips.reloadSoundOutOfAmmo;
mainAudioSource.Play ();
//If out of ammo, hide the bullet renderer in the mag
//Do not show if bullet renderer is not assigned in inspector
if (bulletInMagRenderer != null)
{
bulletInMagRenderer.GetComponent
<SkinnedMeshRenderer> ().enabled = false;
//Start show bullet delay
StartCoroutine (ShowBulletInMag ());
}
}
else
{
//Play diff anim if ammo left
anim.Play ("Reload Ammo Left", 0, 0f);
mainAudioSource.clip = SoundClips.reloadSoundAmmoLeft;
mainAudioSource.Play ();
//If reloading when ammo left, show bullet in mag
//Do not show if bullet renderer is not assigned in inspector
if (bulletInMagRenderer != null)
{
bulletInMagRenderer.GetComponent
<SkinnedMeshRenderer> ().enabled = true;
}
}
//Restore ammo when reloading
currentAmmo = ammo;
outOfAmmo = false;
}
*/
//Enable bullet in mag renderer after set amount of time
private IEnumerator ShowBulletInMag () {
//Wait set amount of time before showing bullet in mag
yield return new WaitForSeconds (showBulletInMagDelay);
bulletInMagRenderer.GetComponent<SkinnedMeshRenderer> ().enabled = true;
}
//Show light when shooting, then disable after set amount of time
private IEnumerator MuzzleFlashLight ()
{
muzzleflashLight.enabled = true;
yield return new WaitForSeconds (lightDuration);
muzzleflashLight.enabled = false;
}
//Check current animation playing
private void AnimationCheck ()
{
//Check if reloading
//Check both animations
if (anim.GetCurrentAnimatorStateInfo (0).IsName ("Reload Out Of Ammo") ||
anim.GetCurrentAnimatorStateInfo (0).IsName ("Reload Ammo Left"))
{
isReloading = true;
}
else
{
isReloading = false;
}
//Check if inspecting weapon
if (anim.GetCurrentAnimatorStateInfo (0).IsName ("Inspect"))
{
isInspecting = true;
}
else
{
isInspecting = false;
}
}
}
// ----- Low Poly FPS Pack Free Version -----