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
| using UnityEngine;
using TMPro;
public class GunSystem : MonoBehaviour
{
//Gun stats
public int damage;
public float timeBetweenShooting, spread, range, reloadTime, timeBetweenShots;
public int magazineSize, bulletsPerTap;
public bool allowButtonHold;
int bulletsLeft, bulletsShot;
//bools
bool shooting, readyToShoot, reloading;
//Reference
public Camera fpsCam;
public Transform attackPoint;
public RaycastHit rayHit;
public LayerMask whatIsEnemy;
//Graphics
//public GameObject muzzleFlash, bulletHoleGraphic;
//public CamShake camShake;
//public float camShakeMagnitude, camShakeDuration;
public TextMeshProUGUI text;
private void Awake()
{
bulletsLeft = magazineSize;
readyToShoot = true;
}
private void Update()
{
MyInput();
//SetText
text.SetText(bulletsLeft + " / " + magazineSize);
Debug.DrawRay(fpsCam.transform.position, transform.forward * 10, Color.green);
}
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();
//Shoot
if (readyToShoot && shooting && !reloading && bulletsLeft > 0)
{
bulletsShot = bulletsPerTap;
Shoot();
}
}
private void Shoot()
{
readyToShoot = false;
//Spread
float x = Random.Range(-spread, spread);
float y = Random.Range(-spread, spread);
//Calculate Direction with Spread
Vector3 direction = fpsCam.transform.forward + new Vector3(x, y, 0);
Debug.DrawRay(fpsCam.transform.position, direction * 10, Color.green);
//RayCast
if (Physics.Raycast(fpsCam.transform.position, direction, out rayHit, range, whatIsEnemy))
{
Debug.Log(rayHit.collider.name);
if (rayHit.collider.CompareTag("Enemy"))
//rayHit.collider.GetComponent<ShootingAi>().TakeDamage(damage);
Debug.Log(rayHit.collider.name);
}
//ShakeCamera
//camShake.Shake(camShakeDuration, camShakeMagnitude);
//Graphics
//Instantiate(bulletHoleGraphic, rayHit.point, Quaternion.Euler(0, 180, 0));
//Instantiate(muzzleFlash, attackPoint.position, Quaternion.identity);
bulletsLeft--;
bulletsShot--;
Invoke("ResetShot", timeBetweenShooting);
if (bulletsShot > 0 && bulletsLeft > 0)
Invoke("Shoot", timeBetweenShots);
}
private void ResetShot()
{
readyToShoot = true;
}
private void Reload()
{
reloading = true;
Invoke("ReloadFinished", reloadTime);
}
private void ReloadFinished()
{
bulletsLeft = magazineSize;
reloading = false;
}
}
|