-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gun.cs
143 lines (120 loc) · 3.6 KB
/
Gun.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
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
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(MuzzleFlash))]
public class Gun : MonoBehaviour
{
public enum FireMode { Auto, Burst, Single };
public FireMode fireMode;
public Transform[] projectileSpawn;
public Projectile projectile;
public float msBetweenShots = 100f;
public float muzzleVelocity = 35f;
public int burstCount;
public int projectilsPerMag;
public float reloadTime = 0.3f;
[Header("Recoil")]
public Vector2 kickMinMax = new Vector2(0.5f, 0.2f);
public Vector2 recoilAngleMinMax = new Vector2(3f, 5f);
public float recoilMoveSettleTime = 0.1f;
public float recoilRotationSettleTime = 0.1f;
[Header("Effect")]
public Transform shell;
public Transform shellEjector;
MuzzleFlash muzzleFlash;
float nextShotTime;
bool triggerReleasedSinceLastShot;
int shotsRemainingInBurst;
int projectilesRemainingInMag;
bool isReloading;
Vector3 recoilSmoothDampVelocity;
float recoilRotationSmoothDampVelocity;
float recoilAngle;
void Start()
{
muzzleFlash = GetComponent<MuzzleFlash>();
shotsRemainingInBurst = burstCount;
projectilesRemainingInMag = projectilsPerMag;
}
void LateUpdate()
{
// animate recoil
transform.localPosition = Vector3.SmoothDamp(transform.localPosition, Vector3.zero, ref recoilSmoothDampVelocity, 0.1f);
recoilAngle = Mathf.SmoothDamp(recoilAngle, 0, ref recoilRotationSmoothDampVelocity, recoilRotationSettleTime);
transform.localEulerAngles = transform.localEulerAngles + Vector3.left * recoilAngle;
if (!isReloading && projectilesRemainingInMag == 0)
{
Reload();
}
}
void Shoot()
{
if (!isReloading && Time.time > nextShotTime && projectilesRemainingInMag > 0)
{
if (fireMode == FireMode.Burst)
{
if (shotsRemainingInBurst == 0) return;
shotsRemainingInBurst--;
}
else if (fireMode == FireMode.Single)
{
if (!triggerReleasedSinceLastShot) return;
}
for (var i = 0; i < projectileSpawn.Length; i++)
{
if (projectilesRemainingInMag == 0) break;
projectilesRemainingInMag--;
nextShotTime = Time.time + msBetweenShots / 1000;
var newProjectile = Instantiate(projectile, projectileSpawn[i].position, projectileSpawn[i].rotation) as Projectile;
newProjectile.SetSpeed(muzzleVelocity);
}
Instantiate(shell, shellEjector.position, shellEjector.rotation);
muzzleFlash.Activate();
transform.localPosition -= Vector3.forward * Random.Range(kickMinMax.x, kickMinMax.y);
recoilAngle += Random.Range(recoilAngleMinMax.x, recoilAngleMinMax.y);
recoilAngle = Mathf.Clamp(recoilAngle, 0, 30);
}
}
public void Reload()
{
if (!isReloading && projectilesRemainingInMag != projectilsPerMag)
{
StartCoroutine(AnimateReload());
}
}
IEnumerator AnimateReload()
{
isReloading = true;
yield return new WaitForSeconds(0.2f);
float reloadSpeed = 1 / reloadTime;
float percent = 0;
while (percent < 1)
{
percent += Time.deltaTime * reloadSpeed;
var maxReloadAngle = 30;
var interpolation = (-Mathf.Pow(percent, 2) + percent) * 4;
var reloadAngle = Mathf.Lerp(0, maxReloadAngle, interpolation);
Vector3 initialRot = transform.localEulerAngles;
transform.localEulerAngles = initialRot + Vector3.left * reloadAngle;
yield return null;
}
isReloading = false;
projectilesRemainingInMag = projectilsPerMag;
}
public void Aim(Vector3 aimPoint)
{
if (!isReloading)
{
transform.LookAt(aimPoint);
}
}
public void OnTriggerHold()
{
Shoot();
triggerReleasedSinceLastShot = false;
}
public void OnTriggerRelease()
{
triggerReleasedSinceLastShot = true;
shotsRemainingInBurst = burstCount;
}
}