mirror of
https://github.com/DerTyp7/defrain-shooter-unity.git
synced 2025-10-29 12:52:07 +01:00
Grenades
[+] added + grenade.cs script + grenades can be thrown + grenades "explode" and spawn "explosion" (currently only a 50% transparent cube) - NEEDS TO BE TESTED => BUG FIXED
This commit is contained in:
51
Assets/Scripts/Weapons/Grenade.cs
Normal file
51
Assets/Scripts/Weapons/Grenade.cs
Normal file
@@ -0,0 +1,51 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class Grenade : MonoBehaviour
|
||||
{
|
||||
[SerializeField] GameObject explodeParticle;
|
||||
[SerializeField] Weapon weapon;
|
||||
private float countdown;
|
||||
|
||||
void Start() {
|
||||
countdown = weapon.Timer;
|
||||
}
|
||||
|
||||
void Update() {
|
||||
// If grenade has been thrown and countdown is over 0 and grenade has not exploded yet
|
||||
if (weapon.HasBeenThrown && !weapon.HasExploded) {
|
||||
// Decrease timer by 1 second
|
||||
countdown -= Time.deltaTime;
|
||||
// If countdown get to 0... BOOM!:
|
||||
if(countdown <= 0) {
|
||||
// Lets grenade explode
|
||||
Explode();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* - Spawn explosion particles and add force to nearby objects - */
|
||||
private void Explode() {
|
||||
// Spawns explosion particle
|
||||
GameObject spawnedExplosion = Instantiate(explodeParticle, transform.position, transform.rotation);
|
||||
// Destroys explosion particle after on second
|
||||
Destroy(spawnedExplosion, 1);
|
||||
// Gets all collider that are in a sphere around the grenade
|
||||
Collider[] colliders = Physics.OverlapSphere(transform.position, weapon.GrenadeRadius);
|
||||
// Iterate over all colliders found in radius
|
||||
foreach(Collider nearbyObject in colliders) {
|
||||
// Get Rigidbody from nearby object and...
|
||||
Rigidbody rb = nearbyObject.GetComponent<Rigidbody>();
|
||||
// if rigidbody exists...
|
||||
if(rb != null) {
|
||||
// adds force to nearby objects
|
||||
rb.AddExplosionForce(weapon.ExplosionForce, transform.position, weapon.GrenadeRadius);
|
||||
}
|
||||
}
|
||||
weapon.HasExploded = true;
|
||||
// Destroys grenade
|
||||
Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Weapons/Grenade.cs.meta
Normal file
11
Assets/Scripts/Weapons/Grenade.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: efa52c67441299049b2e2634d9e3e534
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -5,12 +5,14 @@ using Mirror;
|
||||
using TMPro;
|
||||
public class Shoot : NetworkBehaviour
|
||||
{
|
||||
|
||||
[SerializeField] WeaponManager weaponManager; // For throwing grenade
|
||||
[SerializeField] GameObject muzzle;
|
||||
[SerializeField] ProcedualAnimationController shootAnim;
|
||||
[SerializeField] GameObject weaponHolder;
|
||||
[SerializeField] Camera mCamera;
|
||||
[SerializeField] bool limitAmmunition = true;
|
||||
|
||||
|
||||
private Weapon weapon;
|
||||
private RaycastHit crosshairHitPoint;
|
||||
private Vector3 _pointDirection;
|
||||
@@ -48,21 +50,37 @@ public class Shoot : NetworkBehaviour
|
||||
{
|
||||
weapon.GetComponent<BoxCollider>().enabled = false;
|
||||
}
|
||||
if (weapon.AllowAction && weapon.CurrentAmmunition > 0)
|
||||
{
|
||||
shootAnim.Recoil(0.1f);
|
||||
}
|
||||
CmdFireBullet();
|
||||
|
||||
// If current weapon kind is a rifle or pistole
|
||||
string weaponKindString = weapon.WeaponKind.ToString();
|
||||
if(weaponKindString == "Rifle" || weaponKindString == "Pistole") {
|
||||
if (weapon.AllowAction && weapon.CurrentAmmunition > 0) {
|
||||
shootAnim.Recoil(0.1f);
|
||||
}
|
||||
// Shoot Weapon
|
||||
CmdFireBullet();
|
||||
} // If current weapon kind is grenade
|
||||
else if(weaponKindString == "Grenade"){
|
||||
// Throw Grenade
|
||||
throwGrenade();
|
||||
} // If current weapon kind is kinfe
|
||||
else {
|
||||
// Throw hands (punch)
|
||||
}
|
||||
}
|
||||
if (Input.GetButtonDown("Reload")) {
|
||||
updateCanvas = true;
|
||||
CmdReloadWeapon();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void throwGrenade() {
|
||||
Debug.Log("ThrowGrenade!");
|
||||
// Throws grenade with dropForce
|
||||
weapon.HasBeenThrown = true;
|
||||
weaponManager.dropWeapon(weapon.DropForce);
|
||||
}
|
||||
|
||||
[Command]
|
||||
private void CmdReloadWeapon() {
|
||||
if (weapon.AllowAction && limitAmmunition) {
|
||||
|
||||
@@ -9,10 +9,9 @@ public class Weapon : MonoBehaviour
|
||||
Rifle, Pistole, Knife, Grenade
|
||||
}
|
||||
[SerializeField] weaponKinds weaponKind;
|
||||
[SerializeField] bool active = false;
|
||||
[SerializeField] float dropForce = 10f;
|
||||
[SerializeField] int damage = 0;
|
||||
[SerializeField] float firerate = 0;
|
||||
[SerializeField] int roundsPerShot = 1;
|
||||
[SerializeField] float recoilStrength = 0;
|
||||
[SerializeField] int currentAmmunition = 0;
|
||||
[SerializeField] int magazinSize = 0;
|
||||
@@ -22,12 +21,17 @@ public class Weapon : MonoBehaviour
|
||||
[SerializeField] Animator weaponAnimator;
|
||||
[SerializeField] Transform gunRightREF;
|
||||
[SerializeField] Transform gunLeftREF;
|
||||
[Header("Grenade")]
|
||||
[SerializeField] bool isGrenade = false;
|
||||
[SerializeField] float timer = 2f;
|
||||
[SerializeField] float explosionForce = 2f;
|
||||
[SerializeField] float grenadeRadius = 3f;
|
||||
[SerializeField] bool hasExploded = false;
|
||||
[SerializeField] bool hasBeenThrown = false;
|
||||
|
||||
public bool Active { get => active; set => active = value; }
|
||||
public weaponKinds WeaponKind { get => weaponKind; }
|
||||
public int Damage { get => damage; set => damage = value; }
|
||||
public float Firerate { get => firerate; set => firerate = value; }
|
||||
public int RoundsPerShot { get => roundsPerShot; set => roundsPerShot = value; }
|
||||
public float RecoilStrength { get => recoilStrength; set => recoilStrength = value; }
|
||||
public int CurrentAmmunition { get => currentAmmunition; set => currentAmmunition = value; }
|
||||
public int MagazinSize { get => magazinSize; set => magazinSize = value; }
|
||||
@@ -37,10 +41,18 @@ public class Weapon : MonoBehaviour
|
||||
public Animator WeaponAnimator { get => weaponAnimator; }
|
||||
public Transform GunLeftREF { get => gunLeftREF; }
|
||||
public Transform GunRightREF { get => gunRightREF; }
|
||||
public float Timer { get => timer; set => timer = value; }
|
||||
public float GrenadeRadius { get => grenadeRadius; set => grenadeRadius = value; }
|
||||
public bool HasExploded { get => hasExploded; set => hasExploded = value; }
|
||||
public bool IsGrenade { get => isGrenade; set => isGrenade = value; }
|
||||
public bool HasBeenThrown { get => hasBeenThrown; set => hasBeenThrown = value; }
|
||||
public float ExplosionForce { get => explosionForce; set => explosionForce = value; }
|
||||
public float DropForce { get => dropForce; set => dropForce = value; }
|
||||
|
||||
private void Start()
|
||||
{
|
||||
private void Start() {
|
||||
CurrentAmmunition = MagazinSize;
|
||||
if (weaponKind == weaponKinds.Grenade) { IsGrenade = true; }
|
||||
if (IsGrenade) { weaponKind = weaponKinds.Grenade; }
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -29,20 +29,20 @@ public class WeaponManager : NetworkBehaviour
|
||||
lastWeaponIndex = currentWeaponIndex;
|
||||
activeWeapons[currentWeaponIndex].SetActive(false);
|
||||
switchWeapon(-1);
|
||||
activeWeapons[currentWeaponIndex].SetActive(true);
|
||||
}
|
||||
else if (Input.GetAxis("Mouse ScrollWheel") < 0f) { // Scroll down
|
||||
lastWeaponIndex = currentWeaponIndex;
|
||||
activeWeapons[currentWeaponIndex].SetActive(false);
|
||||
switchWeapon(1);
|
||||
activeWeapons[currentWeaponIndex].SetActive(true);
|
||||
}
|
||||
activeWeapons[currentWeaponIndex].SetActive(true);
|
||||
if (Input.GetButtonDown("Interact")) { // e
|
||||
PickupWeapon();
|
||||
|
||||
}else if (Input.GetButtonDown("Drop")) { // q Droping weapon
|
||||
if (activeWeapons[currentWeaponIndex] != null) {
|
||||
dropWeapon(); // Throws weapon away
|
||||
switchWeapon(1);
|
||||
dropWeapon(activeWeapons[currentWeaponIndex].GetComponent<Weapon>().DropForce); // Throws weapon away
|
||||
activeWeapons[currentWeaponIndex].SetActive(true);
|
||||
}
|
||||
}
|
||||
@@ -118,7 +118,7 @@ public class WeaponManager : NetworkBehaviour
|
||||
|
||||
private bool putWeaponInArray(int index, RaycastHit hit) {
|
||||
if (activeWeapons[currentWeaponIndex] != null) {
|
||||
dropWeapon(); // Throws weapon away
|
||||
dropWeapon(activeWeapons[currentWeaponIndex].GetComponent<Weapon>().DropForce); // Throws weapon away
|
||||
}
|
||||
activeWeapons[index] = hit.transform.gameObject;
|
||||
activeWeapons[index].SetActive(true);
|
||||
@@ -131,7 +131,7 @@ public class WeaponManager : NetworkBehaviour
|
||||
return true;
|
||||
}
|
||||
|
||||
private bool dropWeapon() {
|
||||
public bool dropWeapon(float dropForce) {
|
||||
if(currentWeaponIndex != 2) {
|
||||
GameObject currentWeapon = activeWeapons[currentWeaponIndex];
|
||||
currentWeapon.SetActive(true);
|
||||
@@ -142,7 +142,7 @@ public class WeaponManager : NetworkBehaviour
|
||||
currentWeapon.GetComponent<BoxCollider>().enabled = true;
|
||||
currentWeapon.gameObject.transform.SetParent(null);
|
||||
activeWeapons[currentWeaponIndex] = null;
|
||||
|
||||
switchWeapon(1);
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
|
||||
Reference in New Issue
Block a user