using System.Collections; using System.Collections.Generic; using UnityEngine; using Mirror; public class Shoot : NetworkBehaviour { [SerializeField] GameObject muzzle; [SerializeField] ShootAnimation shootAnim; [SerializeField] GameObject gunHoldPos; [SerializeField] GameObject weaponHolder; [SerializeField] GameObject GunRotation; Ammunition ammunition; private Weapon weapon; private RaycastHit crosshairHitPoint; private Camera mCamera; private Vector3 _pointDirection; private Quaternion _lookRotation; private void Start() { if (isLocalPlayer) { mCamera = Camera.main; weapon = weaponHolder.GetComponent(); shootAnim.OnSwitchWeapon(weapon.Firerate); } } private void Update() { if (isLocalPlayer) { if (Input.GetButton("Fire")) { CmdFireBullet(); } if (Input.GetButton("Reload")) { CmdReloadWeapon(); } } } [Command] private void CmdReloadWeapon() { if (GetComponent() != null && weapon.AllowAction) { GetComponent().reloadWeapon(weapon); } } [Command] // This code will be executed on the Server. private void CmdFireBullet() { Debug.Log(mCamera.transform.forward); Vector3 hitpos; RaycastHit hit; Ray 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; } _pointDirection = hitpos - muzzle.transform.position; _lookRotation = Quaternion.LookRotation(_pointDirection); GunRotation.transform.rotation = Quaternion.RotateTowards(GunRotation.transform.rotation, _lookRotation, 1f); if (weapon.AllowAction) // shooting { shootAnimation(); if (Physics.Raycast(muzzle.transform.position, muzzle.transform.forward, out hit) && weapon.CurrentAmmunition > 0) { Debug.DrawLine(muzzle.transform.position, hit.point); Debug.Log("Distance: " + hit.distance); if (hit.transform.gameObject.GetComponent() != null) { Debug.Log("GETROFFEN------------------"); hit.transform.gameObject.GetComponent().RemoveHealth(20); } } if (GetComponent() != null) { GetComponent().subtractAmmunition(weapon); } StartCoroutine(fireRate()); } } [Client] void shootAnimation() { shootAnim.recoil(gunHoldPos, 0.1f); } IEnumerator fireRate() { weapon.AllowAction = false; yield return new WaitForSeconds(60f/weapon.Firerate); weapon.AllowAction = true; } }