mirror of
				https://github.com/DerTyp7/defrain-shooter-unity.git
				synced 2025-10-29 20:52:10 +01:00 
			
		
		
		
	Weapon
idk
This commit is contained in:
		| @@ -2,108 +2,141 @@ using System.Collections; | ||||
| using System.Collections.Generic; | ||||
| using UnityEngine; | ||||
| using Mirror; | ||||
|  | ||||
| using TMPro; | ||||
| public class Shoot : NetworkBehaviour | ||||
| { | ||||
|     [SerializeField] GameObject muzzle; | ||||
|     [SerializeField] ShootAnimation shootAnim; | ||||
|     [SerializeField] GameObject weaponHolder; | ||||
|     [SerializeField] GameObject GunRotation; | ||||
|      | ||||
|     [SerializeField] Camera mCamera; | ||||
|     [SerializeField] bool limitAmmunition = true; | ||||
|  | ||||
|     Ammunition ammunition; | ||||
|     private Weapon weapon; | ||||
|     private RaycastHit crosshairHitPoint; | ||||
|     private Camera mCamera; | ||||
|     private Vector3 _pointDirection; | ||||
|     private Quaternion _lookRotation; | ||||
|     private Vector3 hitpos; | ||||
|     private RaycastHit hit; | ||||
|     private Ray ray; | ||||
|     private void Start() | ||||
|     { | ||||
|         if (isLocalPlayer) | ||||
|         { | ||||
|             mCamera = Camera.main; | ||||
|     private bool updateCanvas = true; | ||||
|     private int curAmmo = 1, totalAmmo = 1; | ||||
|  | ||||
|     public int CurAmmo { get => curAmmo; set => curAmmo = value; } | ||||
|     public int TotalAmmo { get => totalAmmo; set => totalAmmo = value; } | ||||
|  | ||||
|     private void Start() { | ||||
|          | ||||
|         if (isLocalPlayer) { | ||||
|             weapon = weaponHolder.GetComponent<Weapon>(); | ||||
|             shootAnim.OnSwitchWeapon(weapon.Firerate); | ||||
|             curAmmo = weapon.CurrentAmmunition; | ||||
|             totalAmmo = weapon.TotalAmmunition; | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     private void Update() | ||||
|     { | ||||
|         Debug.Log("Test"); | ||||
|         if (isLocalPlayer) | ||||
|         { | ||||
|             if (Input.GetButtonDown("Fire")) | ||||
|             { | ||||
|     private void Update() { | ||||
|         if (isLocalPlayer) { | ||||
|             if (updateCanvas) { | ||||
|                 curAmmo = weapon.CurrentAmmunition; | ||||
|                 totalAmmo = weapon.TotalAmmunition; | ||||
|                 updateCanvas = false; | ||||
|             } | ||||
|             if (Input.GetButtonDown("Fire")) { | ||||
|                 updateCanvas = true; | ||||
|                 CmdFireBullet(); | ||||
|             } | ||||
|             if (Input.GetButtonDown("Reload")) | ||||
|             { | ||||
|             if (Input.GetButtonDown("Reload")) { | ||||
|                 updateCanvas = true; | ||||
|                 CmdReloadWeapon(); | ||||
|             } | ||||
|              | ||||
|         } | ||||
|     } | ||||
|  | ||||
|  | ||||
|     [Command] | ||||
|     private void CmdReloadWeapon() | ||||
|     { | ||||
|         if (GetComponent<Ammunition>() != null && weapon.AllowAction) | ||||
|         { | ||||
|             GetComponent<Ammunition>().reloadWeapon(weapon); | ||||
|     private void CmdReloadWeapon() { | ||||
|         if (weapon.AllowAction && limitAmmunition) { | ||||
|             reloadWeapon(weapon); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|    [Command] | ||||
|     // This code will be executed on the Server. | ||||
|     private void CmdFireBullet() | ||||
|     { | ||||
|         ray = new Ray(mCamera.transform.position, mCamera.transform.forward); | ||||
|         if(Physics.Raycast(ray, out crosshairHitPoint, 5000f)){ | ||||
|  | ||||
|             hitpos = crosshairHitPoint.point; | ||||
|         } | ||||
|         else | ||||
|         { | ||||
|             hitpos = mCamera.transform.position + mCamera.transform.forward * 5000; | ||||
|     private void CmdFireBullet() { | ||||
|         ray = new Ray(mCamera.transform.position, mCamera.transform.forward); // Raycast from Camera | ||||
|         if(Physics.Raycast(ray, out crosshairHitPoint, 5000f)) { // Check if Raycast is beyond 5000 | ||||
|             hitpos = crosshairHitPoint.point; // If hitpoint is under 5000 | ||||
|         } else {  | ||||
|             hitpos = mCamera.transform.position + mCamera.transform.forward * 5000;  | ||||
|         } | ||||
|         _pointDirection = hitpos - muzzle.transform.position; | ||||
|         _lookRotation = Quaternion.LookRotation(_pointDirection); | ||||
|         GunRotation.transform.rotation = Quaternion.RotateTowards(GunRotation.transform.rotation, _lookRotation, 1f); | ||||
|         GunRotation.transform.rotation = Quaternion.RotateTowards(GunRotation.transform.rotation, _lookRotation, 1f); // Point weapon to raycast hitpoint from camera | ||||
|  | ||||
|         if (weapon.AllowAction) | ||||
|         { | ||||
|             if (Physics.Raycast(muzzle.transform.position, muzzle.transform.forward, out hit) && weapon.CurrentAmmunition > 0) | ||||
|             { | ||||
|                 shootAnimation(); | ||||
|         if (weapon.AllowAction) { // If not reloading etc. | ||||
|             if (Physics.Raycast(muzzle.transform.position, muzzle.transform.forward, out hit) && weapon.CurrentAmmunition > 0) { // Raycast from Bullet Exit Point to camera raycast  | ||||
|                 shootAnimation(); // Start Shoot Animation | ||||
|                 Debug.DrawLine(muzzle.transform.position, hit.point); | ||||
|                 Debug.Log(hit.transform.name); | ||||
|                 if (hit.transform.gameObject.GetComponent<Player>() != null) | ||||
|                 { | ||||
|                     Debug.Log("GETROFFEN------------------"); | ||||
|                     hit.transform.gameObject.GetComponent<Player>().RemoveHealth(20); | ||||
|  | ||||
|                 bulletHole(GameObject.CreatePrimitive(PrimitiveType.Sphere), hit); // Creates bullethole where raycast hits | ||||
|  | ||||
|                 if (hit.transform.gameObject.GetComponent<Player>() != null) { // If hit object is a player | ||||
|                     Debug.Log("-->HIT PLAYER: " + hit.transform.name); | ||||
|                     hit.transform.gameObject.GetComponent<Player>().RemoveHealth(weapon.Damage);  | ||||
|                 } | ||||
|             } | ||||
|             if (GetComponent<Ammunition>() != null) | ||||
|             { | ||||
|                 GetComponent<Ammunition>().subtractAmmunition(weapon); | ||||
|             if (limitAmmunition) { | ||||
|                 subtractAmmunition(weapon); // Subtract Ammunition  | ||||
|             } | ||||
|             StartCoroutine(fireRate()); | ||||
|         } | ||||
|          | ||||
|         }         | ||||
|     } | ||||
|     [Client] | ||||
|     void shootAnimation() | ||||
|  | ||||
|     void bulletHole(GameObject holeObject, RaycastHit hit) | ||||
|     { | ||||
|         holeObject.transform.localScale = new Vector3(0.2f, 0.2f, 0.2f); | ||||
|         holeObject.transform.position = hit.point; | ||||
|     } | ||||
|  | ||||
|     [Client] | ||||
|     void shootAnimation() { | ||||
|         shootAnim.recoil(0.1f); | ||||
|     } | ||||
|     IEnumerator fireRate() | ||||
|     { | ||||
|  | ||||
|  | ||||
|     IEnumerator fireRate() { | ||||
|         weapon.AllowAction = false; | ||||
|         yield return new WaitForSeconds(60f/weapon.Firerate); | ||||
|         yield return new WaitForSeconds(60f/weapon.Firerate); // Waits for firerate seconds | ||||
|         weapon.AllowAction = true; | ||||
|     } | ||||
|  | ||||
|     private bool subtractAmmunition(Weapon weapon) { // Subtracts Ammunition from weapon  | ||||
|         if (weapon.CurrentAmmunition > 0) { | ||||
|             weapon.CurrentAmmunition -= 1; | ||||
|             return true; | ||||
|         } | ||||
|         return false; | ||||
|     } | ||||
|  | ||||
|     private bool reloadWeapon(Weapon weapon) {  // Reloads Ammunition from weapon  | ||||
|         if (weapon.AllowAction && weapon.TotalAmmunition > 0) { | ||||
|             weapon.AllowAction = false; | ||||
|             int dif = weapon.MagazinSize - weapon.CurrentAmmunition; | ||||
|             if (weapon.TotalAmmunition >= dif) { | ||||
|                 weapon.CurrentAmmunition += dif; | ||||
|                 weapon.TotalAmmunition -= dif; | ||||
|             } | ||||
|             else { | ||||
|                 weapon.CurrentAmmunition += weapon.TotalAmmunition; | ||||
|                 weapon.TotalAmmunition = 0; | ||||
|             } | ||||
|             weapon.AllowAction = true; | ||||
|             Debug.Log("Reloaded"); | ||||
|             return true; | ||||
|         } | ||||
|         return false; | ||||
|     } | ||||
|  | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -10,7 +10,7 @@ public class Weapon : MonoBehaviour | ||||
|     } | ||||
|     [SerializeField] weaponKinds weaponKind; | ||||
|     [SerializeField] bool active = false; | ||||
|     [SerializeField] float damage = 0; | ||||
|     [SerializeField] int damage = 0; | ||||
|     [SerializeField] float firerate = 0; | ||||
|     [SerializeField] int roundsPerShot = 1; | ||||
|     [SerializeField] float recoilStrength = 0; | ||||
| @@ -22,7 +22,7 @@ public class Weapon : MonoBehaviour | ||||
|  | ||||
|     public bool Active { get => active; set => active = value; } | ||||
|     public weaponKinds WeaponKind { get => weaponKind; set => weaponKind = value; } | ||||
|     public float Damage { get => damage; set => damage = value; } | ||||
|     public int Damage { get => damage; set => damage = value; } | ||||
|     public float Firerate { get => firerate; set => firerate = value; } | ||||
|     public int RoundsPerShot { get => roundsPerShot; set => roundsPerShot = value; } | ||||
|     public float RecoilStrength { get => recoilStrength; set => recoilStrength = value; } | ||||
|   | ||||
| @@ -1,57 +1,64 @@ | ||||
| using System.Collections; | ||||
| using System.Collections.Generic; | ||||
| using UnityEngine; | ||||
| using Mirror; | ||||
|  | ||||
| public class WeaponManager : MonoBehaviour | ||||
|  | ||||
| public class WeaponManager : NetworkBehaviour | ||||
| { | ||||
|     public int currentWeaponIndex = 0; | ||||
|     public List<GameObject> allWeapons = new List<GameObject>(); | ||||
|     public GameObject[] activeWeapons; | ||||
|  | ||||
|     [SerializeField] GameObject cam; | ||||
|     [SerializeField] Camera 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; | ||||
|                     }                     | ||||
|                 }                 | ||||
|     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; | ||||
|             } | ||||
|         } | ||||
|         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)) | ||||
|         { | ||||
|             Debug.DrawLine(cam.transform.position, hit.point); | ||||
|             if (hit.transform.tag == "Weapon") // 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; | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     private bool searchInArray(GameObject[] arr, GameObject searchObj) | ||||
|     { | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Noah4ever
					Noah4ever