-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pistol.cs
98 lines (75 loc) · 2.27 KB
/
Pistol.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
using System.Collections;
using UnityEngine;
public class Pistol : MonoBehaviour
{
[SerializeField] public float damage = 10f;
[SerializeField] public float range = 100f;
[SerializeField] public float fireRate = 15f;
[SerializeField] public float impactForce = 40f;
public int maxAmmo = 9;
private int currentAmmo;
public float reloadTime = 1f;
public bool isReloading = false;
[SerializeField] public Camera fpsCam;
//public ParticleSystem muzzleFlash; if adding muzzle flash
[SerializeField] public GameObject impactEffect;
private float nextTimeToFire = 0f;
public Animator animator;
private void Start()
{
currentAmmo = maxAmmo;
}
private void OnEnable()
{
isReloading = false;
animator.SetBool("PistolReloading", false);
}
void Update()
{
if(isReloading)
{
return;
}
if (currentAmmo <= 0)
{
StartCoroutine(Reload());
return;
}
if (Input.GetButtonDown("Fire1") && Time.time >= nextTimeToFire)
{
nextTimeToFire = Time.time + 1f / fireRate;
Shot();
}
}
public IEnumerator Reload()
{
isReloading = true;
Debug.Log("Reloading...");
animator.SetBool("PistolReloading", true);
yield return new WaitForSeconds(reloadTime - .25f);
animator.SetBool("PistolReloading", false);
yield return new WaitForSeconds(.25f);
currentAmmo = maxAmmo;
isReloading = false;
}
public void Shot()
{
//muzzleFlash.Play();
RaycastHit hit;
if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
{
Debug.Log(hit.transform.name);
EnemyPlayer enemyPlayer = hit.transform.GetComponent<EnemyPlayer>();
if (enemyPlayer != null)
{
enemyPlayer.TakeDamage(damage);
}
if (hit.rigidbody != null)
{
hit.rigidbody.AddForce(-hit.normal * impactForce);
}
GameObject hitImpact = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
Destroy(hitImpact, 2f);
}
}
}