JoshButner

Henchman - Grabber.cs

Mar 10th, 2022 (edited)
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.81 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Sirenix.OdinInspector;
  5.  
  6. [RequireComponent(typeof(Rigidbody))]
  7. public class Grabber : MonoBehaviour
  8. {
  9.     [Tooltip("The point at the center of the grabbing")]
  10.     [Required, SceneObjectsOnly]
  11.     public Transform grabCenter;
  12.     [Tooltip("Left or Right hand")]
  13.     public Hand whichHand;
  14.  
  15.     // Horrible no good very bad way to do this, this is currently only referenced in Grabber.cs, MainMenu.cs, and InGameMenu.cs
  16.     public static bool isToggleGrip = true;
  17.     private bool wasToggleGrip = true;
  18.  
  19.     // The list of all objects the hand could grab if it tried to grab right now
  20.     private HashSet<Grabbable> objectsCanGrab = new HashSet<Grabbable>();
  21.     // The currently grabbed object
  22.     private Grabbable grabbed = null;
  23.     // Tracks whether the grab button is down or not
  24.     private bool isGrabButtonDown = false;
  25.  
  26.     void Start(){
  27.         // Register the input
  28.         //string inputID = whichHand == Hand.Left ? "Oculus_CrossPlatform_PrimaryHandTrigger" : "Oculus_CrossPlatform_SecondaryHandTrigger";
  29.         string inputID = whichHand == Hand.Left ? ControlManager.Instance(gameObject).NamedInputs["LeftGrab"] : ControlManager.Instance(gameObject).NamedInputs["RightGrab"];
  30.  
  31.         ControlManager.Instance(gameObject).Inputs[inputID].onPress.AddListener(HandleGrabPressed);
  32.         ControlManager.Instance(gameObject).Inputs[inputID].onRelease.AddListener(HandleGrabReleased);
  33.     }
  34.  
  35.     void FixedUpdate(){
  36.         if(isToggleGrip != wasToggleGrip){
  37.             isGrabButtonDown = false;
  38.             Ungrab();
  39.         }
  40.  
  41.         // If the grab button is down and we haven't grabbed anything, keep trying until we let go or grab something
  42.         if(isGrabButtonDown && grabbed == null){
  43.             TryGrab();
  44.         }
  45.         wasToggleGrip = isToggleGrip;
  46.     }
  47.  
  48.  
  49.     // Keep track of grabbable objects
  50.     void OnTriggerEnter(Collider other){
  51.         Grabbable potentialGrabbable = other.GetComponentInParent<Grabbable>();
  52.         if(potentialGrabbable != null){
  53.             objectsCanGrab.Add(potentialGrabbable);
  54.         }
  55.     }
  56.  
  57.     // Deregister grabbable objects
  58.     void OnTriggerExit(Collider other){
  59.         Grabbable potentialGrabbable = other.GetComponentInParent<Grabbable>();
  60.         if(potentialGrabbable != null){
  61.             objectsCanGrab.Remove(potentialGrabbable);
  62.         }
  63.     }
  64.  
  65.     // This allows other scripts to force the player to let go of all objects, this is useful in cases such as when we teleport the player and don't want to let them bring objects with them.
  66.     public void ForceDropObjects(){
  67.         Ungrab();
  68.     }
  69.  
  70.     // Finds which object we grab when we try to
  71.     private Grabbable GetObjToGrab(){
  72.         Grabbable currentBest = null;
  73.  
  74.         foreach(Grabbable current in objectsCanGrab){
  75.             if(currentBest == null){
  76.               // First make sure that if there's any object possible to grab, we get at least one
  77.               currentBest = current;
  78.             }else if(current.priorityLevel > currentBest.priorityLevel){
  79.               // Then get objects with higher priority level
  80.               // This lets us label some objects as core to mechanics, thus making it easier to grab them vs less important objects
  81.               currentBest = current;
  82.             }else if((current.location.position - grabCenter.position).magnitude < (currentBest.location.position - grabCenter.position).magnitude){
  83.               // Finally grab the object which the grabber is closes to the center of
  84.               currentBest = current;
  85.             }
  86.         }
  87.  
  88.         return currentBest;
  89.     }
  90.  
  91.     // This is called when the grab button is pressed
  92.     private void HandleGrabPressed(ControlManager.AxisInput input){
  93.         if(!isToggleGrip)
  94.         {
  95.             isGrabButtonDown = true;
  96.             TryGrab();
  97.         }else{
  98.             isGrabButtonDown = !isGrabButtonDown;
  99.             if(isGrabButtonDown)
  100.             {
  101.                 TryGrab();
  102.             }else{
  103.                 Ungrab();
  104.             }
  105.         }
  106.     }
  107.  
  108.     // This is called when the grab button is pressed
  109.     private void HandleGrabReleased(ControlManager.AxisInput input){
  110.         if(!isToggleGrip)
  111.         {
  112.             isGrabButtonDown = false;
  113.             Ungrab();
  114.         }else{
  115.             // Do Nothing
  116.         }
  117.     }
  118.  
  119.  
  120.     // Tries to grab an object, returns whether it succeeded
  121.     private bool TryGrab(){
  122.         grabbed = GetObjToGrab();
  123.  
  124.         if(grabbed != null){
  125.             grabbed.Grabbed(this);
  126.             return true;
  127.         }else{
  128.             return false;
  129.         }
  130.     }
  131.  
  132.     // Lets go of an object
  133.     private void Ungrab(){
  134.       if(grabbed != null){
  135.           grabbed.Ungrabbed(this);
  136.           grabbed = null;
  137.       }
  138.     }
  139. }
  140.  
Advertisement
Add Comment
Please, Sign In to add comment