-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathShootingRayCast.cs
78 lines (71 loc) · 2.31 KB
/
ShootingRayCast.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
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using UnityEngine;
public class ShootingRayCast : MonoBehaviour
{
[Header("Settings")]
[SerializeField] private float _Damage = 20;
[SerializeField] private float _ShootDistance = 50;
[SerializeField] private string _EnemyTag = "Enemy";
[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()
{
RaycastHit hit;
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit, _ShootDistance))
if (hit.transform.tag == _EnemyTag)
{
hit.transform.GetComponent<Health>().DoDamage(_Damage);
}
}
}