Advertisement
Guest User

Untitled

a guest
Aug 17th, 2023
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.61 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System.Collections;
  5. using Unity.Netcode;
  6. using System;
  7.  
  8. public class Gun : NetworkBehaviour
  9. {
  10. // How often you can shoot the gun
  11. public float fireRate = 15f;
  12.  
  13. // The prefab of the bullet
  14. public GameObject projectile;
  15.  
  16. // The position of the bullet
  17. private Vector3 destination;
  18.  
  19. // Where the bullet gets spawned from
  20. [SerializeField] private Transform InistialTransform;
  21.  
  22. // All of these should be pretty self explanitory
  23. public int maxAmmo = 15;
  24. private int currentAmmo;
  25. public float reloadTime = 1f;
  26. private bool isReloading = false;
  27.  
  28. //The camera that the player sees through and the impact effect when the bullet hits something.
  29. public Camera fpsCam;
  30. public GameObject impactEffect;
  31.  
  32. // The animator for reloading
  33. public Animator animator;
  34.  
  35. // Keeps track of when the player can shoot next
  36. private float nextTimeToFire = 0f;
  37.  
  38. // Makes sure that they start with a full mag.
  39. private void Start()
  40. {
  41. currentAmmo = maxAmmo;
  42. }
  43.  
  44. // Makes sure they dont start reloading.
  45. void OnEnable()
  46. {
  47. isReloading = false;
  48. animator.SetBool("Reloading", false);
  49. }
  50.  
  51. // Checks some player stuff.
  52. void Update()
  53. {
  54. if (isReloading)
  55. {
  56. return;
  57. }
  58.  
  59. if (Input.GetKeyDown(KeyCode.R) && IsOwner)
  60. {
  61. StartCoroutine(Reload());
  62. return;
  63. }
  64.  
  65. if (Input.GetButton("Fire1") && Time.time >= nextTimeToFire && IsOwner)
  66. {
  67. nextTimeToFire = Time.time + 1f / fireRate;
  68. if (currentAmmo > -1)
  69. {
  70. Shoot();
  71. }
  72. }
  73. }
  74.  
  75. // Reloads the gun
  76. IEnumerator Reload()
  77. {
  78. isReloading = true;
  79. Debug.Log("Reloading...");
  80.  
  81. animator.SetBool("Reloading", true);
  82.  
  83. yield return new WaitForSeconds(reloadTime - .25f);
  84. animator.SetBool("Reloading", false);
  85. yield return new WaitForSeconds(1f);
  86.  
  87.  
  88. currentAmmo = maxAmmo;
  89. isReloading = false;
  90. }
  91.  
  92. // Shoots the gun
  93. void Shoot()
  94. {
  95. currentAmmo--;
  96.  
  97. RaycastHit hit;
  98. if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit))
  99. {
  100. Debug.Log(hit.transform.name);
  101.  
  102. ShootProjectile();
  103.  
  104. GameObject impactGO = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
  105. Destroy(impactGO, 2f);
  106. }
  107. }
  108.  
  109. // Starts the instantiating bullet process.
  110. void ShootProjectile()
  111. {
  112. Ray ray = fpsCam.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
  113. RaycastHit hit;
  114.  
  115. if (Physics.Raycast(ray, out hit))
  116. {
  117. destination = hit.point;
  118.  
  119. } else
  120. {
  121. destination = ray.GetPoint(1000);
  122. }
  123.  
  124. //Starts the instantiating bullet process.
  125. SpawnBulletServerRPC(InistialTransform.position, InistialTransform.rotation);
  126. }
  127.  
  128. // This actually instantiates and spawns the bullet where I want.
  129. [ServerRpc]
  130. private void SpawnBulletServerRPC (Vector3 position, Quaternion rotation, ServerRpcParams serverRpcParams = default)
  131. {
  132. //Instantiates the bullet
  133. GameObject InstantiatedBullet = Instantiate(projectile, position, rotation);
  134.  
  135. //Spawns the bullet through the network
  136. InstantiatedBullet.GetComponent<NetworkObject>().Spawn();
  137. }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement