Advertisement
Placido_GDD

RaycastBuckshot

Mar 16th, 2022 (edited)
850
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.95 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Photon.Pun;
  5.  
  6. public class BuckShot : Gun
  7. {
  8.     [SerializeField] Camera cam;
  9.     PhotonView PV;
  10.     List<Ray> shotRays = new List<Ray>();
  11.     [SerializeField] Transform gunBarrel;
  12.  
  13.     private void Awake()
  14.     {
  15.         baseDamage = gunInfo.damage;
  16.         PV = GetComponent<PhotonView>();
  17.         ammoCount = maxAmmo;
  18.         ammoCounter.text = ammoCounter.text + ammoCount.ToString();
  19.     }
  20.     public override void Use()
  21.     {
  22.         if (Time.time < lastShot + timeBetweenShots)
  23.             return;
  24.         if (ammoCount > 0)
  25.         {
  26.             Shoot();
  27.             lastShot = Time.time;
  28.             ammoCount--;
  29.             ammoCounter.text = ammoCount.ToString();
  30.         }
  31.         if (ammoCount <= 0)
  32.         {
  33.             ammoCount = 0;
  34.             ammoCounter.text = ammoCount.ToString();
  35.         }
  36.     }
  37.  
  38.     public override void Reload()
  39.     {
  40.         ammoCount = maxAmmo;
  41.         ammoCounter.text = ammoCount.ToString();
  42.     }
  43.     void Shoot()
  44.     {
  45.        for (int i = 0; i < shotsPerAttack; i++)
  46.         {
  47.             Ray ray = cam.ViewportPointToRay(GetOverallSpread() + new Vector2(0.5f,0.5f));
  48.             ray.origin = cam.transform.position;
  49.             shotRays.Add(ray);
  50.         }
  51.  
  52.        foreach (Ray ray in shotRays)
  53.         {
  54.             if (Physics.Raycast(ray, out RaycastHit hit))
  55.             {
  56.                 hit.collider.gameObject.GetComponent<IDamageable>()?.TakeDamage(((GunInfo)itemInfo).damage);    
  57.                 PV.RPC("RPC_Shoot", RpcTarget.All, hit.point, hit.normal); //this will run on all in game clients
  58.             }
  59.         }
  60.         //Ray ray = cam.ViewportPointToRay(new Vector3(0.5f, 0.5f));
  61.         //ray.origin = cam.transform.position;
  62.         //if (Physics.Raycast(ray, out RaycastHit hit))
  63.         //{
  64.             //hit.collider.gameObject.GetComponent<IDamageable>()?.TakeDamage(((GunInfo)itemInfo).damage);
  65.             //PV.RPC("RPC_Shoot", RpcTarget.All, hit.point, hit.normal); //this will run on all in game clients
  66.         //}
  67.     }
  68.    
  69.     Vector2 GetIndividualSpread()
  70.     {
  71.         return Random.insideUnitCircle * individualSpread;
  72.     }
  73.  
  74.     Vector2 GetOverallSpread()
  75.     {
  76.         return Random.insideUnitCircle * overallSpread;
  77.     }
  78.  
  79.  
  80.     [PunRPC]
  81.     void RPC_Shoot(Vector3 hitPos, Vector3 hitNormal)
  82.     {
  83.         Collider[] colliders = Physics.OverlapSphere(hitPos, 0.3f);
  84.         if (colliders.Length != 0)
  85.         {
  86.             GameObject bulletImpactObj = Instantiate(bulletImpactPrefab, hitPos + hitNormal * 0.001f, Quaternion.LookRotation(hitNormal, Vector3.up) * bulletImpactPrefab.transform.rotation); //instantiate a bullet impact prefab on the obj hit facing towards the player
  87.             Destroy(bulletImpactObj, 5.0f);
  88.             bulletImpactObj.transform.SetParent(colliders[0].transform); //parents the instantiated bulletimpact Obj
  89.  
  90.         }
  91.  
  92.     }
  93. }
  94.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement