-
Notifications
You must be signed in to change notification settings - Fork 2
/
Shooting.cs
75 lines (68 loc) · 2.24 KB
/
Shooting.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Shooting : MonoBehaviour
{
[Header("Settings")]
[SerializeField] ObjectPool _ObjectPool = null;
[SerializeField] private GameObject _BulletPrefab = null;
[SerializeField] private GameObject _ShootPoint = null;
[Header("Semi")]
[SerializeField] private int _SemiAutomaticBulletAmount = 3;
[SerializeField] private float _SemiShootSpeed = 0.2f;
[Header("Automatic")]
[SerializeField] private float _SecondsBetweenShots = 0.5f;
private enum ShootModes { SingleShot, SemiAutomatic, Automatic }
[SerializeField] private ShootModes _ShootMode = ShootModes.SingleShot;
private bool _CheckSingleShot;
private float _Timer;
private bool _LockShooting;
void Update()
{
if (Input.GetMouseButton(0))
{
switch (_ShootMode)
{
case ShootModes.SingleShot:
if (!_CheckSingleShot)
Shoot();
_CheckSingleShot = true;
break;
case ShootModes.SemiAutomatic:
if (!_CheckSingleShot && !_LockShooting)
StartCoroutine(SemiShot());
_CheckSingleShot = true;
break;
case ShootModes.Automatic:
_Timer += 1 * Time.deltaTime;
if (_Timer >= _SecondsBetweenShots)
{
Shoot();
_Timer = 0;
}
break;
}
}
if (Input.GetMouseButtonUp(0))
{
_CheckSingleShot = false;
}
}
IEnumerator SemiShot()
{
_LockShooting = true;
for (int i = 0; i < _SemiAutomaticBulletAmount; i++)
{
Shoot();
yield return new WaitForSeconds(_SemiShootSpeed);
}
_LockShooting = false;
}
void Shoot()
{
GameObject bullet = _ObjectPool.GetObject(_BulletPrefab, true);
bullet.SetActive(true);
bullet.transform.position = _ShootPoint.transform.position;
bullet.transform.rotation = _ShootPoint.transform.rotation;
}
}