mirror of
https://github.com/DerTyp7/defrain-shooter-unity.git
synced 2025-10-29 20:52:10 +01:00
Merge branch 'main' into Player-Animation
This commit is contained in:
46
Assets/Scripts/Weapons/Shoot.cs
Normal file
46
Assets/Scripts/Weapons/Shoot.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using Mirror;
|
||||
|
||||
public class Shoot : NetworkBehaviour
|
||||
{
|
||||
[SerializeField] GameObject muzzle;
|
||||
private void Update()
|
||||
{
|
||||
if (isLocalPlayer)
|
||||
{
|
||||
if (Input.GetKeyDown(KeyCode.Mouse0))
|
||||
{
|
||||
//CmdFireBullet();
|
||||
//RpcOnFire();
|
||||
CmdFireBullet();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Command]
|
||||
// This code will be executed on the server.
|
||||
private void CmdFireBullet()
|
||||
{
|
||||
GameObject dedplayer;
|
||||
RaycastHit hit;
|
||||
if (Physics.Raycast(muzzle.transform.position, muzzle.transform.forward, out hit))
|
||||
{
|
||||
if (hit.transform.gameObject.GetComponent<Player>() != null)
|
||||
{
|
||||
dedplayer = hit.transform.gameObject;
|
||||
dedplayer.GetComponent<Player>().RemoveHealth(20);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
[Client]
|
||||
// This code will be executed on the Client.
|
||||
void RpcOnFire()
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
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: bc5bc2b49bd326e4db460a6a3af59311
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
84
Assets/Scripts/Weapons/Weapon.cs
Normal file
84
Assets/Scripts/Weapons/Weapon.cs
Normal file
@@ -0,0 +1,84 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class Weapon : MonoBehaviour
|
||||
{
|
||||
public enum weaponKinds
|
||||
{
|
||||
Rifle, Pistole, Knife, Granade
|
||||
}
|
||||
[SerializeField] weaponKinds weaponKind;
|
||||
[SerializeField] bool active = false;
|
||||
[SerializeField] float damage = 0;
|
||||
[SerializeField] float firerate = 0;
|
||||
[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;
|
||||
|
||||
public bool Active { get => active; set => active = value; }
|
||||
public weaponKinds WeaponKind { get => weaponKind; set => weaponKind = 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;
|
||||
}
|
||||
|
||||
}
|
||||
11
Assets/Scripts/Weapons/Weapon.cs.meta
Normal file
11
Assets/Scripts/Weapons/Weapon.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c98f5c47a8b7dd64f86fd6f42c4d6e5e
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
63
Assets/Scripts/Weapons/WeaponManager.cs
Normal file
63
Assets/Scripts/Weapons/WeaponManager.cs
Normal file
@@ -0,0 +1,63 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class WeaponManager : MonoBehaviour
|
||||
{
|
||||
public int currentWeaponIndex = 0;
|
||||
public List<GameObject> allWeapons = new List<GameObject>();
|
||||
public GameObject[] activeWeapons;
|
||||
|
||||
[SerializeField] GameObject cam;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
activeWeapons = new GameObject[4];
|
||||
}
|
||||
|
||||
void Update() {
|
||||
if(Input.GetAxis("Mouse ScrollWheel") > 0f){ // Scroll up
|
||||
if (currentWeaponIndex <= 0)
|
||||
{ currentWeaponIndex = activeWeapons.Length; }
|
||||
else { currentWeaponIndex--; }
|
||||
}
|
||||
if (Input.GetAxis("Mouse ScrollWheel") < 0f){ // Scroll down
|
||||
if (currentWeaponIndex >= activeWeapons.Length)
|
||||
{ currentWeaponIndex = 0; }
|
||||
else { currentWeaponIndex++; }
|
||||
}
|
||||
if (Input.GetButton("Interact")) // e
|
||||
{
|
||||
if (Physics.Raycast(cam.transform.position, cam.transform.forward, out RaycastHit hit))
|
||||
{
|
||||
Debug.DrawLine(cam.transform.position, hit.point);
|
||||
if (allWeapons.Contains(hit.transform.gameObject) && !searchInArray(activeWeapons, hit.transform.gameObject)) // If Object is a weapon and the weapon is not in the current active weapons
|
||||
{
|
||||
switch (hit.transform.GetComponent<Weapon>().WeaponKind.ToString()) // Adding weapon to inventory slot
|
||||
{
|
||||
case "Rifle": activeWeapons[0] = hit.transform.gameObject; break;
|
||||
case "Pistole": activeWeapons[1] = hit.transform.gameObject; break;
|
||||
case "Knife": activeWeapons[2] = hit.transform.gameObject; break;
|
||||
case "Granade": activeWeapons[3] = hit.transform.gameObject; break;
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (Input.GetButton("Drop")) // q Droping weapon
|
||||
{
|
||||
activeWeapons[currentWeaponIndex] = null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private bool searchInArray(GameObject[] arr, GameObject searchObj)
|
||||
{
|
||||
foreach(GameObject obj in arr)
|
||||
{
|
||||
if (obj == searchObj) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
11
Assets/Scripts/Weapons/WeaponManager.cs.meta
Normal file
11
Assets/Scripts/Weapons/WeaponManager.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 98dadd30ba25ec34db3b45d0dca2827b
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user