-
Notifications
You must be signed in to change notification settings - Fork 0
/
GrenadeThrower.cs
65 lines (56 loc) · 2.31 KB
/
GrenadeThrower.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GrenadeThrower : MonoBehaviour
{
public GameObject[] grenade;
public Camera fpsCam;
public float throwingTime = 1.5f;
public float throwForce = 10f;
private int grenadeType = 0;
private WeaponManger weaponManager;
private AudioSource throwSound;
// Start is called before the first frame update
void Start()
{
/*weaponManager = transform.gameObject.GetComponent<WeaponManger>();
weaponManager.GetPlayerUI().Weapon.SetGrenadeAmount(GrenadeType.Explosive, weaponManager.Ammo.Grenade(GrenadeType.Explosive));
weaponManager.GetPlayerUI().Weapon.SetGrenadeAmount(GrenadeType.Smoke, weaponManager.Ammo.Grenade(GrenadeType.Smoke));*/
throwSound = gameObject.GetComponent<AudioSource>();
}
// Update is called once per frame
void Update()
{
}
public void Throw()
{
gameObject.SetActive(true);
StartCoroutine(_Throw());
}
private IEnumerator _Throw()
{
if (throwSound)
throwSound.Play();
// wait for throw animation to finish
yield return new WaitForSeconds(1.5f);
ThrowGrenade();
gameObject.SetActive(false);
}
private void ThrowGrenade()
{
GrenadeType type = grenade[grenadeType].transform.GetComponent<Grenade>().Type;
GameObject currentGrenade = Instantiate(grenade[grenadeType], transform.position, transform.rotation);
Rigidbody rb = currentGrenade.GetComponent<Rigidbody>();
rb.AddForce(fpsCam.transform.up * (throwForce / 3), ForceMode.VelocityChange);
rb.AddForce(fpsCam.transform.forward * throwForce, ForceMode.VelocityChange);
/*if (weaponManager.Ammo.Grenade(type) > 0)
{
GameObject currentGrenade = Instantiate(grenade[grenadeType], transform.position, transform.rotation);
Rigidbody rb = currentGrenade.GetComponent<Rigidbody>();
rb.AddForce(transform.up * (throwForce / 3), ForceMode.VelocityChange);
rb.AddForce(transform.forward * throwForce, ForceMode.VelocityChange);
weaponManager.Ammo.UpdateGrenade(type, -1);
weaponManager.GetPlayerUI().Weapon.SetGrenadeAmount(type, weaponManager.Ammo.Grenade(type));
}*/
}
}