mirror of
				https://github.com/DerTyp7/defrain-shooter-unity.git
				synced 2025-10-29 20:52:10 +01:00 
			
		
		
		
	Merge branch 'weapons-without-scriptableobjects'
This commit is contained in:
		
							
								
								
									
										8
									
								
								Assets/Scripts/Weapons.meta
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								Assets/Scripts/Weapons.meta
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,8 @@ | ||||
| fileFormatVersion: 2 | ||||
| guid: 07f5bfb9bc3f0134e92e220ccbea4949 | ||||
| folderAsset: yes | ||||
| DefaultImporter: | ||||
|   externalObjects: {} | ||||
|   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
	 Noah4ever
					Noah4ever