mirror of
https://github.com/DerTyp7/defrain-shooter-unity.git
synced 2025-10-29 20:52:10 +01:00
Network Weapons
+ started weapons with Mirror
This commit is contained in:
50
Assets/Scripts/Weapons/Ammunition.cs
Normal file
50
Assets/Scripts/Weapons/Ammunition.cs
Normal file
@@ -0,0 +1,50 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class Ammunition : MonoBehaviour
|
||||
{
|
||||
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public bool subtractAmmunition(Weapon weapon)
|
||||
{
|
||||
if (weapon.CurrentAmmunition > 0) {
|
||||
weapon.CurrentAmmunition -= weapon.RoundsPerShot;
|
||||
Debug.Log(weapon.CurrentAmmunition + " - " + weapon.RoundsPerShot);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool reloadWeapon(Weapon weapon)
|
||||
{
|
||||
if (weapon.AllowAction && weapon.TotalAmmunition > 0) {
|
||||
weapon.AllowAction = false;
|
||||
int dif = weapon.MagazinSize - weapon.CurrentAmmunition;
|
||||
|
||||
if (weapon.TotalAmmunition >= dif) {
|
||||
weapon.CurrentAmmunition += dif;
|
||||
weapon.TotalAmmunition -= dif;
|
||||
}
|
||||
else {
|
||||
weapon.CurrentAmmunition += weapon.TotalAmmunition;
|
||||
weapon.TotalAmmunition = 0;
|
||||
}
|
||||
weapon.AllowAction = true;
|
||||
Debug.Log("Reloaded");
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Weapons/Ammunition.cs.meta
Normal file
11
Assets/Scripts/Weapons/Ammunition.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 323a821a5fef23a45aa5333ce1f55202
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
22
Assets/Scripts/Weapons/Reload.cs
Normal file
22
Assets/Scripts/Weapons/Reload.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using Mirror;
|
||||
public class Reload : NetworkBehaviour
|
||||
{
|
||||
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
if (isLocalPlayer)
|
||||
{
|
||||
if (Input.GetButton("Reload"))
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
11
Assets/Scripts/Weapons/Reload.cs.meta
Normal file
11
Assets/Scripts/Weapons/Reload.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9d6f405c993960a419018706eaaee8c8
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
76
Assets/Scripts/Weapons/Shoot.cs
Normal file
76
Assets/Scripts/Weapons/Shoot.cs
Normal file
@@ -0,0 +1,76 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using Mirror;
|
||||
|
||||
public class Shoot : NetworkBehaviour
|
||||
{
|
||||
[SerializeField] GameObject muzzle;
|
||||
[SerializeField] GameObject weaponHolder;
|
||||
private Weapon weapon;
|
||||
|
||||
Ammunition ammunition;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
weapon = weaponHolder.GetComponent<Weapon>();
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (isLocalPlayer)
|
||||
{
|
||||
if (Input.GetButton("Fire"))
|
||||
{
|
||||
CmdFireBullet();
|
||||
}
|
||||
if (Input.GetButton("Reload"))
|
||||
{
|
||||
CmdReloadWeapon();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
[Command]
|
||||
private void CmdReloadWeapon()
|
||||
{
|
||||
if (GetComponent<Ammunition>() != null && weapon.AllowAction)
|
||||
{
|
||||
GetComponent<Ammunition>().reloadWeapon(weapon);
|
||||
}
|
||||
}
|
||||
|
||||
[Command]
|
||||
// This code will be executed on the Server.
|
||||
private void CmdFireBullet()
|
||||
{
|
||||
if (weapon.AllowAction)
|
||||
{
|
||||
if (Physics.Raycast(muzzle.transform.position, muzzle.transform.forward, out RaycastHit hit) && weapon.CurrentAmmunition > 0)
|
||||
{
|
||||
Debug.DrawLine(muzzle.transform.position, hit.point);
|
||||
Debug.Log("Geshooted BITCH");
|
||||
if (hit.transform.gameObject.GetComponent<Player>() != null)
|
||||
{
|
||||
Debug.Log("GETROFFEN------------------");
|
||||
hit.transform.gameObject.GetComponent<Player>().RemoveHealth(20);
|
||||
}
|
||||
}
|
||||
if (GetComponent<Ammunition>() != null)
|
||||
{
|
||||
GetComponent<Ammunition>().subtractAmmunition(weapon);
|
||||
}
|
||||
StartCoroutine(fireRate());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
IEnumerator fireRate()
|
||||
{
|
||||
weapon.AllowAction = false;
|
||||
yield return new WaitForSeconds(weapon.Firerate);
|
||||
weapon.AllowAction = true;
|
||||
}
|
||||
|
||||
}
|
||||
11
Assets/Scripts/Weapons/Shoot.cs.meta
Normal file
11
Assets/Scripts/Weapons/Shoot.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0f43c52a1834dcb4bab807bc0cbddec2
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -12,73 +12,29 @@ public class Weapon : MonoBehaviour
|
||||
[SerializeField] bool active = false;
|
||||
[SerializeField] float damage = 0;
|
||||
[SerializeField] float firerate = 0;
|
||||
[SerializeField] int roundsPerShot = 1;
|
||||
[SerializeField] float recoilStrength = 0;
|
||||
[SerializeField] int currentAmmunition = 0;
|
||||
[SerializeField] int magazinSize = 0;
|
||||
[SerializeField] int totalAmmunition = 0;
|
||||
[SerializeField] ParticleSystem flash;
|
||||
[SerializeField] GameObject bulletExit;
|
||||
|
||||
private bool allowShoot = true;
|
||||
[SerializeField] bool allowAction = true;
|
||||
|
||||
public bool Active { get => active; set => active = value; }
|
||||
public weaponKinds WeaponKind { get => weaponKind; set => weaponKind = value; }
|
||||
|
||||
public float 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; }
|
||||
public int TotalAmmunition { get => totalAmmunition; set => totalAmmunition = value; }
|
||||
public GameObject BulletExit { get => bulletExit; set => bulletExit = value; }
|
||||
public bool AllowAction { get => allowAction; set => allowAction = value; }
|
||||
|
||||
private void Start()
|
||||
{
|
||||
currentAmmunition = magazinSize;
|
||||
}
|
||||
private void FixedUpdate()
|
||||
{
|
||||
if (Input.GetButton("Fire") && allowShoot && currentAmmunition > 0)
|
||||
{
|
||||
fire();
|
||||
StartCoroutine(fireRate());
|
||||
currentAmmunition--;
|
||||
}
|
||||
if (Input.GetButton("Reload"))
|
||||
{
|
||||
if (allowShoot && totalAmmunition > 0)
|
||||
{
|
||||
allowShoot = false;
|
||||
int dif = magazinSize - currentAmmunition;
|
||||
|
||||
if(totalAmmunition >= dif) {
|
||||
currentAmmunition += dif;
|
||||
totalAmmunition -= dif;
|
||||
}
|
||||
else{
|
||||
currentAmmunition += totalAmmunition;
|
||||
totalAmmunition = 0;
|
||||
}
|
||||
allowShoot = true;
|
||||
}
|
||||
}
|
||||
if (Input.GetButton("Aim"))
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
private void fire()
|
||||
{
|
||||
allowShoot = false;
|
||||
flash.Play();
|
||||
RaycastHit hit;
|
||||
|
||||
if(Physics.Raycast(bulletExit.transform.position,bulletExit.transform.forward, out hit))
|
||||
{
|
||||
Debug.DrawLine(bulletExit.transform.position, hit.point);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
IEnumerator fireRate()
|
||||
{
|
||||
allowShoot = false;
|
||||
yield return new WaitForSeconds(firerate);
|
||||
allowShoot = true;
|
||||
CurrentAmmunition = MagazinSize;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -26,6 +26,8 @@ public class WeaponManager : MonoBehaviour
|
||||
{ currentWeaponIndex = 0; }
|
||||
else { currentWeaponIndex++; }
|
||||
}
|
||||
|
||||
|
||||
if (Input.GetButton("Interact")) // e
|
||||
{
|
||||
if (Physics.Raycast(cam.transform.position, cam.transform.forward, out RaycastHit hit))
|
||||
|
||||
Reference in New Issue
Block a user