Files
defrain-shooter-unity/Assets/Scripts/Weapons/WeaponManager.cs
Noah4ever f9527c17fd Weapon pickup
+ started pickup weapon script
2021-11-15 12:53:19 +01:00

71 lines
2.2 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Mirror;
public class WeaponManager : NetworkBehaviour
{
public int currentWeaponIndex = 0;
public GameObject[] activeWeapons;
[SerializeField] Camera cam;
private void Awake()
{
activeWeapons = new GameObject[4];
}
void Update() {
if (isLocalPlayer) {
if(Input.GetAxis("Mouse ScrollWheel") > 0f){ // Scroll up
if (currentWeaponIndex <= 0)
{ currentWeaponIndex = activeWeapons.Length; }
else { currentWeaponIndex--; }
}else if (Input.GetAxis("Mouse ScrollWheel") < 0f){ // Scroll down
if (currentWeaponIndex >= activeWeapons.Length)
{ currentWeaponIndex = 0; }
else { currentWeaponIndex++; }
}
if (Input.GetButton("Interact")) // e
{
CmdPickupWeapon();
}else if (Input.GetButton("Drop")) // q Droping weapon
{
activeWeapons[currentWeaponIndex] = null;
}
}
}
[Command]
private void CmdPickupWeapon() {
if (Physics.Raycast(cam.transform.position, cam.transform.forward, out RaycastHit hit))
{
if (hit.transform.tag == "Weapon") // If Object is a weapon and the weapon is not in the current active weapons
{
Destroy(hit.transform.gameObject);
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 "Grenade": activeWeapons[3] = hit.transform.gameObject; break;
default: break;
}
}
}
}
private bool isInArray(GameObject[] arr, GameObject searchObj)
{
foreach(GameObject obj in arr)
{
if (obj == searchObj) return true;
}
return false;
}
}