Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using System.Collections;
- using Unity.Netcode;
- using System;
- public class Gun : NetworkBehaviour
- {
- // How often you can shoot the gun
- public float fireRate = 15f;
- // The prefab of the bullet
- public GameObject projectile;
- // The position of the bullet
- private Vector3 destination;
- // Where the bullet gets spawned from
- [SerializeField] private Transform InistialTransform;
- // All of these should be pretty self explanitory
- public int maxAmmo = 15;
- private int currentAmmo;
- public float reloadTime = 1f;
- private bool isReloading = false;
- //The camera that the player sees through and the impact effect when the bullet hits something.
- public Camera fpsCam;
- public GameObject impactEffect;
- // The animator for reloading
- public Animator animator;
- // Keeps track of when the player can shoot next
- private float nextTimeToFire = 0f;
- // Makes sure that they start with a full mag.
- private void Start()
- {
- currentAmmo = maxAmmo;
- }
- // Makes sure they dont start reloading.
- void OnEnable()
- {
- isReloading = false;
- animator.SetBool("Reloading", false);
- }
- // Checks some player stuff.
- void Update()
- {
- if (isReloading)
- {
- return;
- }
- if (Input.GetKeyDown(KeyCode.R) && IsOwner)
- {
- StartCoroutine(Reload());
- return;
- }
- if (Input.GetButton("Fire1") && Time.time >= nextTimeToFire && IsOwner)
- {
- nextTimeToFire = Time.time + 1f / fireRate;
- if (currentAmmo > -1)
- {
- Shoot();
- }
- }
- }
- // Reloads the gun
- IEnumerator Reload()
- {
- isReloading = true;
- Debug.Log("Reloading...");
- animator.SetBool("Reloading", true);
- yield return new WaitForSeconds(reloadTime - .25f);
- animator.SetBool("Reloading", false);
- yield return new WaitForSeconds(1f);
- currentAmmo = maxAmmo;
- isReloading = false;
- }
- // Shoots the gun
- void Shoot()
- {
- currentAmmo--;
- RaycastHit hit;
- if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit))
- {
- Debug.Log(hit.transform.name);
- ShootProjectile();
- GameObject impactGO = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
- Destroy(impactGO, 2f);
- }
- }
- // Starts the instantiating bullet process.
- void ShootProjectile()
- {
- Ray ray = fpsCam.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
- RaycastHit hit;
- if (Physics.Raycast(ray, out hit))
- {
- destination = hit.point;
- } else
- {
- destination = ray.GetPoint(1000);
- }
- //Starts the instantiating bullet process.
- SpawnBulletServerRPC(InistialTransform.position, InistialTransform.rotation);
- }
- // This actually instantiates and spawns the bullet where I want.
- [ServerRpc]
- private void SpawnBulletServerRPC (Vector3 position, Quaternion rotation, ServerRpcParams serverRpcParams = default)
- {
- //Instantiates the bullet
- GameObject InstantiatedBullet = Instantiate(projectile, position, rotation);
- //Spawns the bullet through the network
- InstantiatedBullet.GetComponent<NetworkObject>().Spawn();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement