daily pastebin goal
36%
SHARE
TWEET

Untitled

a guest Mar 18th, 2019 53 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using VRTK;
  5.  
  6. public class Gun : VRTK_InteractableObject
  7. {
  8.  
  9.  
  10.     [Header("Game Objects")]
  11.     public GameObject gun;
  12.     public GameObject leftController;
  13.     public GameObject rightController;
  14.     public GameObject magazine;
  15.     public Slide slide;
  16.     public GameObject trigger;
  17.     public Transform shell;
  18.  
  19.     [Header("Animation")]
  20.     public Animation firingAnimation;
  21.     public float animationSpeed;
  22.  
  23.     [Header("Sound Effect")]
  24.     public AudioClip gunShotSound;
  25.     public float gunShotVolume;
  26.  
  27.     [Header("Weapon Recoil")]
  28.     public float kickup;
  29.     public float kickback;
  30.  
  31.     private float minTriggerSqueeze = -10f;
  32.     private float maxTriggerSqueeze = 45f;
  33.  
  34.     private AudioSource source;
  35.  
  36.     private Collider slideCollider;
  37.  
  38.     private VRTK_ControllerEvents controllerEvents;
  39.  
  40.     public override void StartUsing(VRTK_InteractUse currentUsingObject)
  41.     {
  42.         base.StartUsing(currentUsingObject);
  43.         if (!slide.lockBack)
  44.         {
  45.             if (GetAmmo() > 0)
  46.             {
  47.                 shoot();
  48.                 slide.Fire();
  49.             }
  50.             else
  51.             {
  52.                 slide.Fire();
  53.                 slide.lockBack = true;
  54.             }
  55.         }
  56.         StopUsing();
  57.  
  58.     }
  59.  
  60.     private void ToggleCollision(Rigidbody objRB, Collider objCol, bool state)
  61.     {
  62.         objRB.isKinematic = !state;
  63.         objCol.isTrigger = !state;
  64.     }
  65.  
  66.     private void ToggleSlide(bool state)
  67.     {
  68.         if (!state)
  69.         {
  70.             slide.ForceStopInteracting();
  71.         }
  72.         slide.enabled = state;
  73.         slide.isGrabbable = state;
  74.         slideCollider.isTrigger = state;
  75.     }
  76.  
  77.     public override void StopUsing(VRTK_InteractUse previousUsingObject = null, bool resetUsingObjectState = true)
  78.     {
  79.         base.StopUsing(previousUsingObject, resetUsingObjectState);
  80.     }
  81.  
  82.     private void Awake()
  83.     {
  84.         source = gun.GetComponent<AudioSource>();
  85.  
  86.         slideCollider = slide.GetComponent<Collider>();
  87.  
  88.         IgnoreColliders(magazine.GetComponents<Collider>());
  89.         IgnoreColliders(slide.GetComponents<Collider>());
  90.         IgnoreColliders(slide.GetComponents<Collider>(), magazine.GetComponents<Collider>());
  91.     }
  92.  
  93.     public new void Update()
  94.     {
  95.  
  96.         gun.transform.localRotation = Quaternion.Slerp(gun.transform.localRotation, Quaternion.identity, Time.deltaTime * 5);
  97.         gun.transform.localPosition = Vector3.Lerp(gun.transform.localPosition, Vector3.zero, Time.deltaTime * 5);
  98.  
  99.  
  100.         if (controllerEvents)
  101.         {
  102.             var pressure = (maxTriggerSqueeze * controllerEvents.GetTriggerAxis()) - minTriggerSqueeze;
  103.             trigger.transform.localEulerAngles = new Vector3(0f, -pressure, 0f);
  104.         }
  105.         else
  106.         {
  107.             trigger.transform.localEulerAngles = new Vector3(0f, minTriggerSqueeze, 0f);
  108.         }
  109.     }
  110.  
  111.     private void shoot()
  112.     {
  113.         Transform shellTransform = Instantiate(shell, transform.position, transform.rotation);
  114.         shellTransform.Translate(new Vector3(0.03f, 0.05f, 0), Space.Self);
  115.         IgnoreSlideColliders(shellTransform.GetComponents<Collider>());
  116.         shellTransform.GetComponent<Rigidbody>().AddRelativeForce(new Vector3(20f, 150f, 100f));
  117.         source.PlayOneShot(gunShotSound, gunShotVolume);
  118.         foreach (AnimationState state in firingAnimation)
  119.         {
  120.             state.speed = animationSpeed;
  121.         }
  122.         firingAnimation.Play();
  123.         AddRecoil();
  124.         DecreaseAmmo(1);
  125.     }
  126.  
  127.  
  128.     public override void Grabbed(VRTK_InteractGrab currentGrabbingObject)
  129.     {
  130.         base.Grabbed(currentGrabbingObject);
  131.  
  132.         controllerEvents = currentGrabbingObject.GetComponent<VRTK_ControllerEvents>();
  133.         controllerEvents.TouchpadPressed += dropMag;
  134.  
  135.         ToggleSlide(true);
  136.         ToggleCollision(GetComponent<Rigidbody>(), GetComponent<Collider>(), false);
  137.  
  138.         //Limit hands grabbing when picked up
  139.         if (VRTK_DeviceFinder.GetControllerHand(currentGrabbingObject.controllerEvents.gameObject) == SDK_BaseController.ControllerHand.Left)
  140.         {
  141.             allowedTouchControllers = AllowedController.LeftOnly;
  142.             allowedUseControllers = AllowedController.LeftOnly;
  143.             slide.allowedGrabControllers = AllowedController.RightOnly;
  144.         }
  145.         else if (VRTK_DeviceFinder.GetControllerHand(currentGrabbingObject.controllerEvents.gameObject) == SDK_BaseController.ControllerHand.Right)
  146.         {
  147.             allowedTouchControllers = AllowedController.RightOnly;
  148.             allowedUseControllers = AllowedController.RightOnly;
  149.             slide.allowedGrabControllers = AllowedController.LeftOnly;
  150.         }
  151.     }
  152.  
  153.     public override void Ungrabbed(VRTK_InteractGrab previousGrabbingObject)
  154.     {
  155.         base.Ungrabbed(previousGrabbingObject);
  156.         controllerEvents.TouchpadPressed -= dropMag;
  157.         ToggleCollision(GetComponent<Rigidbody>(), GetComponent<Collider>(), true);
  158.  
  159.         ToggleSlide(false);
  160.  
  161.         //Unlimit hands
  162.         allowedTouchControllers = AllowedController.Both;
  163.         allowedUseControllers = AllowedController.Both;
  164.         slide.allowedGrabControllers = AllowedController.Both;
  165.  
  166.         controllerEvents = null;
  167.     }
  168.  
  169.     private void AddRecoil()
  170.     {
  171.         gun.transform.RotateAround(GetGrabbingObject().transform.position, transform.forward, -kickup);
  172.  
  173.         Vector3 localPos = gun.transform.localPosition;
  174.         localPos.x -= kickback;
  175.         gun.transform.localPosition = localPos;
  176.     }
  177.  
  178.     public void dropMag(object sender, ControllerInteractionEventArgs e)
  179.     {
  180.         Debug.Log("Drop Mag");
  181.         Rigidbody rb = magazine.GetComponent<Rigidbody>();
  182.         magazine.transform.parent = null;
  183.         rb.isKinematic = false;
  184.         rb.AddRelativeForce(new Vector3(0f, -200f, 0f));
  185.         magazine.GetComponent<Collider>().enabled = true;
  186.         magazine.GetComponent<Magazine>().enabled = false;
  187.         magazine = null;
  188.     }
  189.  
  190.     public void DecreaseAmmo(int amount)
  191.     {
  192.         if (magazine == null) return;
  193.  
  194.         Magazine magazineScript = magazine.GetComponent<Magazine>();
  195.         magazineScript.DecreaseAmmo(amount);
  196.     }
  197.  
  198.     public int GetAmmo()
  199.     {
  200.         if (magazine == null) return 0;
  201.  
  202.         Magazine magazineScript = magazine.GetComponent<Magazine>();
  203.         return magazineScript.GetAmmo();
  204.     }
  205.  
  206.     public void IgnoreColliders(Collider[] colliders)
  207.     {
  208.         Collider[] thisColliders = GetComponents<Collider>();
  209.         foreach (Collider collider in colliders)
  210.         {
  211.             foreach (Collider thisCollider in thisColliders)
  212.             {
  213.                 Physics.IgnoreCollision(collider, thisCollider, true);
  214.             }
  215.         }
  216.     }
  217.  
  218.     public void IgnoreColliders(Collider[] colliders, Collider[] thisColliders)
  219.     {
  220.         foreach (Collider collider in colliders)
  221.         {
  222.             foreach (Collider thisCollider in thisColliders)
  223.             {
  224.                 Physics.IgnoreCollision(collider, thisCollider, true);
  225.             }
  226.         }
  227.     }
  228.  
  229.     public void IgnoreSlideColliders(Collider[] colliders)
  230.     {
  231.         Collider[] thisColliders = slide.GetComponents<Collider>();
  232.         foreach (Collider collider in colliders)
  233.         {
  234.             foreach (Collider thisCollider in thisColliders)
  235.             {
  236.                 Physics.IgnoreCollision(collider, thisCollider, true);
  237.             }
  238.         }
  239.     }
  240. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top