-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGun.cs
79 lines (67 loc) · 2.3 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
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Gun : MonoBehaviour
{
[Header("References")]
[SerializeField] GunData gunData;
[SerializeField] Transform cam;
[SerializeField] Transform muzzle;
[SerializeField] Rigidbody playerRB;
float timeSinceLastShot;
private void Start()
{
PlayerShoot.shootInput += Shoot;
PlayerShoot.reloadInput += StartReload;
}
private void OnDisable() => gunData.reloading = false;
public void StartReload()
{
if (!gunData.reloading && this.gameObject.activeSelf)
{
StartCoroutine(Reload());
}
}
private IEnumerator Reload()
{
gunData.reloading = true;
yield return new WaitForSeconds(gunData.reloadTime);
gunData.currentAmmo = gunData.magSize;
gunData.reloading = false;
}
private bool CanShoot() => !gunData.reloading && timeSinceLastShot > 1f / (gunData.fireRate / 60f);
public void Shoot()
{
//Debug.Log("Shoot Gun!");
if (gunData.currentAmmo > 0)
{
if (CanShoot())
{
//Cambiar segun sea necesario el origen y direccion del disparo
//Calculos con la camara y Efectos con el muzzle
if (Physics.Raycast(cam.position, cam.forward, out RaycastHit hitInfo, gunData.maxDistance))
{
print(hitInfo.transform.name);
IDamageable damageable = hitInfo.transform.GetComponent<IDamageable>();
damageable?.TakeDamage(gunData.damage);
//hit.rigidbody.AddForce(ray.direction * hitForce);
//rb.AddForce(transform.up * jumpForce, ForceMode.Impulse);
playerRB.AddForce(-cam.forward * gunData.nockback, ForceMode.Impulse);
}
gunData.currentAmmo--;
timeSinceLastShot = 0;
onGunShot();
}
}
}
private void Update()
{
timeSinceLastShot += Time.deltaTime;
Debug.DrawRay(cam.position, cam.forward * gunData.maxDistance, Color.blue);
Debug.DrawRay(muzzle.position, cam.forward * gunData.maxDistance, Color.red);
}
private void onGunShot()
{
}
}