Advertisement
world_killer

simple gun

Jan 14th, 2023
746
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.24 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using UnityEngine;
  4. using UnityEngine.InputSystem;
  5.  
  6. #nullable enable
  7. #pragma warning disable CS8618
  8. public class Gun : MonoBehaviour
  9. {
  10.     bool holdingGun;
  11.     int ammo = 10;
  12.  
  13.     Transform? gun;
  14.     Transform gunShotPoint;
  15.     [SerializeField] Transform gunHandPoint;
  16.    
  17.     GameObject flash;
  18.     [SerializeField] GameObject bulletHole;
  19.  
  20.     [SerializeField] AudioClip gunSound;
  21.     [SerializeField] AudioSource soundSource;
  22.  
  23.     [SerializeField] LayerMask layerToCheck;
  24.  
  25.     Vector3 handVelocity;
  26.     Vector3 prevHandPos;
  27.    
  28.     #region buttons
  29.     public void RightGripButton(InputAction.CallbackContext context)
  30.     {
  31.         if (context.phase == InputActionPhase.Started)
  32.             GrabGun();
  33.         else if (context.phase == InputActionPhase.Canceled)
  34.             DropGun();
  35.     }
  36.  
  37.     public void RightTriggerButton(InputAction.CallbackContext context)
  38.     {
  39.         if (!holdingGun) return;
  40.  
  41.         if(context.phase == InputActionPhase.Started)
  42.         {
  43.             ammo--;
  44.             Shoot();
  45.         }
  46.     }
  47.     #endregion
  48.  
  49.     Transform? FindGun()
  50.     {
  51.         Transform? foundGun = null;
  52.  
  53.         GameObject[] guns = GameObject.FindGameObjectsWithTag("Gun");
  54.         float highestAngle = 0.45f;
  55.  
  56.         foreach(GameObject gun in guns)
  57.         {
  58.             Vector3 dir = gun.transform.position - transform.position;
  59.             float angle = Vector3.Dot(-transform.right, dir.normalized);
  60.  
  61.             if (angle < highestAngle) continue;
  62.  
  63.             RaycastHit foundObject;
  64.             if(Physics.Raycast(transform.position, dir.normalized, out foundObject, 10, layerToCheck))
  65.             {
  66.                 if (foundObject.transform == gun.transform)
  67.                     foundGun = foundObject.transform;
  68.             }
  69.         }
  70.  
  71.         return foundGun;
  72.     }
  73.  
  74.     void GrabGun()
  75.     {
  76.         Transform? gun = FindGun();
  77.         if (gun == null) return;
  78.  
  79.         Tween moveGun = ScriptableObject.CreateInstance<Tween>();
  80.         moveGun.Initiate(gun, gunHandPoint, gunHandPoint.position, 0.25f);
  81.         //Tween.Create(gun, gunHandPoint, gunHandPoint.position, 0.25f);
  82.         StartCoroutine(moveGun.Play(true));
  83.        
  84.         holdingGun = true;
  85.         gunShotPoint = gun.Find("GunPoint");
  86.         flash = gunShotPoint.Find("GunFlash").gameObject;
  87.         this.gun = gun;
  88.     }
  89.  
  90.     void DropGun()
  91.     {
  92.         Transform foundGun = transform.Find("gunpos").Find("glock");
  93.         if(foundGun == null) return;
  94.        
  95.         Rigidbody gunBody = foundGun.GetComponent<Rigidbody>();
  96.         gunBody.freezeRotation = false;
  97.         gunBody.constraints = RigidbodyConstraints.None;
  98.         gunBody.useGravity = true;
  99.         gunBody.velocity = handVelocity/Time.deltaTime;
  100.  
  101.         foundGun.parent = null;
  102.         gun = null;
  103.     }
  104.  
  105.     void Shoot()
  106.     {
  107.         RaycastHit hitObject;
  108.         soundSource.Play();
  109.         StartCoroutine(Flash());
  110.  
  111.         if(Physics.Raycast(gunShotPoint.position, gunShotPoint.forward, out hitObject, Mathf.Infinity))
  112.         {
  113.             GameObject newbullethole = Instantiate(bulletHole);
  114.             Transform bulletData = newbullethole.transform;
  115.             bulletData.position = hitObject.point;
  116.             bulletData.LookAt(bulletData.position + hitObject.normal);
  117.             bulletData.parent = hitObject.transform;
  118.  
  119.             Color targetColor = hitObject.transform.GetComponent<MeshRenderer>().materials[0].color;
  120.             newbullethole.GetComponent<MeshRenderer>().materials[1].color = targetColor;
  121.  
  122.             StartCoroutine(RemoveBullethole(bulletData));
  123.         }
  124.     }
  125.  
  126.     IEnumerator Flash()
  127.     {
  128.         flash.SetActive(true);
  129.         flash.transform.Find("Glow").GetComponent<ParticleSystem>().Play();
  130.         flash.transform.Find("Sparks").GetComponent<ParticleSystem>().Play();
  131.  
  132.         yield return new WaitForSeconds(.1f);
  133.         flash.SetActive(false);
  134.     }
  135.  
  136.     IEnumerator RemoveBullethole(Transform bulletHole)
  137.     {
  138.         yield return new WaitForSeconds(3);
  139.         Destroy(bulletHole.gameObject);
  140.     }
  141.  
  142.     private void Update()
  143.     {
  144.         handVelocity = (transform.position - prevHandPos);
  145.         prevHandPos = transform.position;
  146.     }
  147. }
  148.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement