Grenade Data Location

Grenade data is now on Grenade.cs and not on Weapon.cs.
You need both scripts to make a grenade work
This commit is contained in:
Noah4ever
2021-12-09 14:55:06 +01:00
parent e59b767daf
commit 0b7ff6f5f1
6 changed files with 1346 additions and 50 deletions

View File

@@ -104,8 +104,8 @@ GameObject:
- component: {fileID: 732033708985862910} - component: {fileID: 732033708985862910}
- component: {fileID: 6352645931139448055} - component: {fileID: 6352645931139448055}
- component: {fileID: 5204381087217674634} - component: {fileID: 5204381087217674634}
- component: {fileID: 2072484884}
- component: {fileID: 9082234699677627348} - component: {fileID: 9082234699677627348}
- component: {fileID: 2072484884}
m_Layer: 0 m_Layer: 0
m_Name: Grenade m_Name: Grenade
m_TagString: Weapon m_TagString: Weapon
@@ -175,6 +175,21 @@ MonoBehaviour:
grenadeRadius: 3 grenadeRadius: 3
hasExploded: 0 hasExploded: 0
hasBeenThrown: 1 hasBeenThrown: 1
--- !u!114 &9082234699677627348
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 5071598280516985511}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: efa52c67441299049b2e2634d9e3e534, type: 3}
m_Name:
m_EditorClassIdentifier:
explodeParticle: {fileID: 8367325100912176335, guid: a8150377575143e4ba9ff51ba16504aa, type: 3}
weapon: {fileID: 5204381087217674634}
showExplosion: 1
--- !u!54 &2072484884 --- !u!54 &2072484884
Rigidbody: Rigidbody:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@@ -191,20 +206,6 @@ Rigidbody:
m_Interpolate: 0 m_Interpolate: 0
m_Constraints: 0 m_Constraints: 0
m_CollisionDetection: 0 m_CollisionDetection: 0
--- !u!114 &9082234699677627348
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 5071598280516985511}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: efa52c67441299049b2e2634d9e3e534, type: 3}
m_Name:
m_EditorClassIdentifier:
explodeParticle: {fileID: 8367325100912176335, guid: a8150377575143e4ba9ff51ba16504aa, type: 3}
weapon: {fileID: 5204381087217674634}
--- !u!1001 &3607281645661515732 --- !u!1001 &3607281645661515732
PrefabInstance: PrefabInstance:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0

File diff suppressed because it is too large Load Diff

View File

@@ -4,17 +4,30 @@ using UnityEngine;
public class Grenade : MonoBehaviour public class Grenade : MonoBehaviour
{ {
[SerializeField] GameObject explodeParticle;
[SerializeField] Weapon weapon; [Header("Grenade Info")]
[SerializeField] float timer = 2f;
[SerializeField] float explosionForce = 500f;
[SerializeField] float grenadeRadius = 3f;
[SerializeField] bool hasExploded = false;
private float countdown; private float countdown;
[Header("Explosion GameObject")]
[SerializeField] GameObject explodeParticle;
[Header("Scripts")]
[SerializeField] Weapon weapon;
[Header("Debug")]
[SerializeField] bool showExplosion = true;
void Start() { void Start() {
countdown = weapon.Timer; countdown = timer;
} }
void Update() { void Update() {
// If grenade has been thrown and countdown is over 0 and grenade has not exploded yet // If grenade has been thrown and countdown is over 0 and grenade has not exploded yet
if (weapon.HasBeenThrown && !weapon.HasExploded) { if (weapon.HasBeenThrown && !hasExploded) {
// Decrease timer by 1 second // Decrease timer by 1 second
countdown -= Time.deltaTime; countdown -= Time.deltaTime;
// If countdown get to 0... BOOM!: // If countdown get to 0... BOOM!:
@@ -28,13 +41,17 @@ public class Grenade : MonoBehaviour
/* - Spawn explosion particles and add force to nearby objects - */ /* - Spawn explosion particles and add force to nearby objects - */
private void Explode() { private void Explode() {
// Spawns explosion particle if (showExplosion)
GameObject spawnedExplosion = Instantiate(explodeParticle, transform.position, transform.rotation); {
// Destroys explosion particle after on second // Spawns explosion particle
Destroy(spawnedExplosion, 1); 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 // Gets all collider that are in a sphere around the grenade
Collider[] colliders = Physics.OverlapSphere(transform.position, weapon.GrenadeRadius); Collider[] colliders = Physics.OverlapSphere(transform.position, grenadeRadius);
// Iterate over all colliders found in radius // Iterate over all colliders found in radius
foreach(Collider nearbyObject in colliders) { foreach(Collider nearbyObject in colliders) {
// Check if nearby object is a Player and if Collider is not a CharacterController (can be changed to CapsuleCollider) // Check if nearby object is a Player and if Collider is not a CharacterController (can be changed to CapsuleCollider)
@@ -47,11 +64,11 @@ public class Grenade : MonoBehaviour
// if rigidbody exists... // if rigidbody exists...
if (rb != null) { if (rb != null) {
// adds force to nearby objects // adds force to nearby objects
rb.AddExplosionForce(weapon.ExplosionForce, transform.position, weapon.GrenadeRadius); rb.AddExplosionForce(explosionForce, transform.position, grenadeRadius);
} }
} }
} }
weapon.HasExploded = true; hasExploded = true;
// Destroys grenade // Destroys grenade
Destroy(gameObject); Destroy(gameObject);
} }

View File

@@ -46,7 +46,6 @@ public class Shoot : NetworkBehaviour
} }
if (Input.GetButtonDown("Fire")) { if (Input.GetButtonDown("Fire")) {
updateCanvas = true; updateCanvas = true;
// If current weapon kind is a rifle or pistole // If current weapon kind is a rifle or pistole
string weaponKindString = weapon.WeaponKind.ToString(); string weaponKindString = weapon.WeaponKind.ToString();
if(weaponKindString == "Rifle" || weaponKindString == "Pistole") { if(weaponKindString == "Rifle" || weaponKindString == "Pistole") {

View File

@@ -8,6 +8,7 @@ public class Weapon : MonoBehaviour
{ {
Rifle, Pistole, Knife, Grenade Rifle, Pistole, Knife, Grenade
} }
[Header("Weapon Info")]
[SerializeField] weaponKinds weaponKind; [SerializeField] weaponKinds weaponKind;
[SerializeField] float dropForce = 10f; [SerializeField] float dropForce = 10f;
[SerializeField] int damage = 0; [SerializeField] int damage = 0;
@@ -18,18 +19,15 @@ public class Weapon : MonoBehaviour
[SerializeField] int totalAmmunition = 0; [SerializeField] int totalAmmunition = 0;
[SerializeField] GameObject bulletExit; [SerializeField] GameObject bulletExit;
[SerializeField] bool allowAction = true; [SerializeField] bool allowAction = true;
[Header("")]
[SerializeField] Animator weaponAnimator; [SerializeField] Animator weaponAnimator;
[SerializeField] Transform gunRightREF; [SerializeField] Transform gunRightREF;
[SerializeField] Transform gunLeftREF; [SerializeField] Transform gunLeftREF;
[Header("Grenade")] //[Header("Grenade")]
[SerializeField] bool isGrenade = false; private bool hasBeenThrown = false;
[SerializeField] float timer = 2f;
[SerializeField] float explosionForce = 2f;
[SerializeField] float grenadeRadius = 3f;
[SerializeField] bool hasExploded = false;
[SerializeField] bool hasBeenThrown = false;
public weaponKinds WeaponKind { get => weaponKind; } public weaponKinds WeaponKind { get => weaponKind; }
public float DropForce { get => dropForce; set => dropForce = value; }
public int Damage { get => damage; set => damage = value; } public int Damage { get => damage; set => damage = value; }
public float Firerate { get => firerate; set => firerate = value; } public float Firerate { get => firerate; set => firerate = value; }
public float RecoilStrength { get => recoilStrength; set => recoilStrength = value; } public float RecoilStrength { get => recoilStrength; set => recoilStrength = value; }
@@ -41,18 +39,10 @@ public class Weapon : MonoBehaviour
public Animator WeaponAnimator { get => weaponAnimator; } public Animator WeaponAnimator { get => weaponAnimator; }
public Transform GunLeftREF { get => gunLeftREF; } public Transform GunLeftREF { get => gunLeftREF; }
public Transform GunRightREF { get => gunRightREF; } 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 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; CurrentAmmunition = MagazinSize;
if (weaponKind == weaponKinds.Grenade) { IsGrenade = true; }
if (IsGrenade) { weaponKind = weaponKinds.Grenade; }
} }
} }

View File

@@ -122,6 +122,7 @@ public class WeaponManager : NetworkBehaviour
} }
activeWeapons[index] = hit.transform.gameObject; activeWeapons[index] = hit.transform.gameObject;
activeWeapons[index].SetActive(true); activeWeapons[index].SetActive(true);
// \/ Same as in switchWeapon()
currentWeaponIndex = index; currentWeaponIndex = index;
procedualAnimationController.OnSwitchWeapon(activeWeapons[currentWeaponIndex]); procedualAnimationController.OnSwitchWeapon(activeWeapons[currentWeaponIndex]);
shoot.setWeapon(activeWeapons[currentWeaponIndex]); shoot.setWeapon(activeWeapons[currentWeaponIndex]);