This commit is contained in:
Noah4ever
2021-10-19 14:36:53 +02:00
parent 631938fa22
commit 3c7a401487
7 changed files with 330 additions and 120 deletions

View File

@@ -4,43 +4,36 @@ 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] ParticleSystem smoke;
[SerializeField] GameObject bulletExit;
[SerializeField] GameObject camera;
[SerializeField] GameObject cam;
[SerializeField] GameObject model;
private bool allowShoot = true, isReloading = false;
public bool Active { get => active; set => active = value; }
private bool allowShoot = true, isShooting = false;
Animator anim;
/*
*
* + Weapon Pickup
* + Weapon Manage
* + Weapon Inventory
* + Weapon Drop
*
*
*/
public weaponKinds WeaponKind { get => weaponKind; set => weaponKind = value; }
//Animator anim;
private void Start()
{
anim = GetComponent<Animator>();
if (Physics.Raycast(camera.transform.position, camera.transform.forward, out RaycastHit hit))
//anim = GetComponent<Animator>();
currentAmmunition = magazinSize;
if (Physics.Raycast(cam.transform.position, cam.transform.forward, out RaycastHit hit)) // Not Working
{
transform.rotation = Quaternion.Euler(0f, hit.point.y, 0f);
}
@@ -49,34 +42,32 @@ public class Weapon : MonoBehaviour
{
if (Input.GetButton("Fire") && allowShoot && currentAmmunition > 0)
{
anim.Play("USP_Shooting");
isShooting = true;
//anim.Play("USP_Shooting");
StartCoroutine(fireRate());
fire();
currentAmmunition--;
}
else
{
isShooting = false;
}
if (Input.GetButton("Reload"))
{
currentAmmunition = totalAmmunition;
if (isReloading == false && totalAmmunition > 0)
{
isReloading = true;
int dif = magazinSize - currentAmmunition;
if(totalAmmunition >= dif) {
currentAmmunition += dif;
totalAmmunition -= dif;
}
else{
currentAmmunition += totalAmmunition;
totalAmmunition = 0;
}
isReloading = false;
}
}
if (Input.GetButton("Aim")) // Not working properly, maybe Animations are not correct to use
{
anim.Play("USP_Aim");
}
if (Input.GetButton("Interact"))
{
if(Physics.Raycast(camera.transform.position, camera.transform.forward, out RaycastHit hit))
{
Debug.DrawLine(camera.transform.position, hit.point);
if (hit.collider.transform.name == transform.name)
{
Debug.Log("Hit me!");
}
}
//anim.Play("USP_Aim");
}
}

View 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;
}
}

View File

@@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 430a0b8472192ab42b0101154932f4ad
guid: 98dadd30ba25ec34db3b45d0dca2827b
MonoImporter:
externalObjects: {}
serializedVersion: 2

View File

@@ -1,20 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WeaponSwitch : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}