Weapon Switching/pickup/throw

[+] added
+ added pickup weapons
+ added throw weapons
+ added weapon switching
+ scrolling searches for next active weapon

[n] needs to be added
n weapon in correct position after pickup
n after weapon throw -> next active weapon needs to be equipt
n throw/pickup/switch weapon animation
This commit is contained in:
Noah4ever
2021-11-20 15:47:58 +01:00
parent facc42b03f
commit a40aec3874
6 changed files with 256 additions and 118 deletions

View File

@@ -7,64 +7,91 @@ using Mirror;
public class WeaponManager : NetworkBehaviour
{
public int currentWeaponIndex = 0;
public GameObject[] activeWeapons;
private int lastWeaponIndex = 0;
private int counter = 0;
public List<GameObject> activeWeapons = new List<GameObject>();
[SerializeField] GameObject gunHolster;
[SerializeField] Camera cam;
private void Awake()
{
activeWeapons = new GameObject[4];
for(int i = 0; i<4; i++)
{
activeWeapons[i] = null;
activeWeapons.Add(null);
}
}
void Update() {
if (isLocalPlayer) {
counter = 0;
if(Input.GetAxis("Mouse ScrollWheel") > 0f){ // Scroll up
do
{
if (currentWeaponIndex <= 0)
{
currentWeaponIndex = activeWeapons.Length - 1;
}
else
{
lastWeaponIndex = currentWeaponIndex;
counter = 0;
do {
if (currentWeaponIndex <= 0) {
currentWeaponIndex = activeWeapons.Count - 1;
} else {
currentWeaponIndex--;
}
counter++;
//Debug.Log(activeWeapons[currentWeaponIndex]);
if (counter > 10) { break; }
} while (activeWeapons[currentWeaponIndex] == null);
if (lastWeaponIndex != currentWeaponIndex && activeWeapons[currentWeaponIndex] != null)
{
// play switch animation or move weapon (hands) down
foreach (GameObject obj in activeWeapons)
{ // Disable all weapons
if (obj != null) { obj.SetActive(false); }
}
Debug.Log("Set Active (" + currentWeaponIndex + "): " + activeWeapons[currentWeaponIndex].name);
activeWeapons[currentWeaponIndex].SetActive(true);
}
}
else if (Input.GetAxis("Mouse ScrollWheel") < 0f){ // Scroll down
do
{
if (currentWeaponIndex >= activeWeapons.Length - 1)
{
lastWeaponIndex = currentWeaponIndex;
counter = 0;
do {
if (currentWeaponIndex >= activeWeapons.Count - 1) {
currentWeaponIndex = 0;
}
else
{
} else {
currentWeaponIndex++;
}
counter++;
//Debug.Log(activeWeapons[currentWeaponIndex]);
if(counter > 10) { break; }
} while (activeWeapons[currentWeaponIndex] == null);
if (lastWeaponIndex != currentWeaponIndex && activeWeapons[currentWeaponIndex] != null)
{
// play switch animation or move weapon (hands) down
foreach (GameObject obj in activeWeapons)
{ // Disable all weapons
if (obj != null) { obj.SetActive(false); }
}
Debug.Log("Set Active (" + currentWeaponIndex + "): " + activeWeapons[currentWeaponIndex].name);
activeWeapons[currentWeaponIndex].SetActive(true);
}
}
if (Input.GetButton("Interact")) // e
if (Input.GetButtonDown("Interact")) // e
{
CmdPickupWeapon();
}else if (Input.GetButton("Drop")) // q Droping weapon
}else if (Input.GetButtonDown("Drop")) // q Droping weapon
{
activeWeapons[currentWeaponIndex] = null;
// WENN GEDROPT WIRD MUSS DIE N<>CHSTE AKTIVE GSUCHT WERDEn
if(activeWeapons[currentWeaponIndex] != null)
{
activeWeapons[currentWeaponIndex].GetComponent<Rigidbody>().useGravity = true;
activeWeapons[currentWeaponIndex].GetComponent<Rigidbody>().isKinematic = false;
activeWeapons[currentWeaponIndex].transform.position = cam.transform.position;
activeWeapons[currentWeaponIndex].GetComponent<BoxCollider>().enabled = true;
activeWeapons[currentWeaponIndex].GetComponent<Rigidbody>().velocity = cam.transform.forward * 10 + cam.transform.up * 2;
activeWeapons[currentWeaponIndex].gameObject.transform.SetParent(null);
activeWeapons[currentWeaponIndex] = null;
}
}
}
}
[Command]
@@ -73,8 +100,16 @@ public class WeaponManager : NetworkBehaviour
{
if (hit.transform.tag == "Weapon") // If Object is a weapon and the weapon is not in the current active weapons
{
foreach (GameObject obj in activeWeapons) { // Disable all weapons
if (obj != null) { obj.SetActive(false); }
}
hit.transform.parent = gunHolster.transform; // Parent weapon to gunHolster
hit.rigidbody.isKinematic = true;
hit.rigidbody.useGravity = false;
hit.transform.GetComponent<BoxCollider>().enabled = false; // Disable Boxcollider
hit.transform.position = cam.transform.position;
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;