JoshButner

Henchman - SnapGrabbable.cs

Mar 10th, 2022
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.37 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Events;
  5. using Sirenix.OdinInspector;
  6.  
  7. // This is a grabbable which snaps the hand of the player to a snapPoint
  8. public class SnapGrabbable : Grabbable{
  9.     [Tooltip("The grabbable will be treated as if this is its Rigidbody.")]
  10.     [Required, SceneObjectsOnly]
  11.     public Rigidbody grabbableRB;
  12.     [Tooltip("The players hand will snap to this position when this object is grabbed.")]
  13.     [Required, SceneObjectsOnly]
  14.     public Transform snapPoint;
  15.     [Tooltip("If true rotation of the grabbed object will copy rotation of the players hand, otherwise only location will be copied.")]
  16.     public bool grabWithRotation = true;
  17.  
  18.     // Events that allow other scripts to know when an object is grabbed or released
  19.     public GrabbedEvent OnGrabbed;
  20.     public GrabbedEvent OnUngrabbed;
  21.  
  22.  
  23.     private AvatarOverrideDriver avatarDriver;
  24.  
  25.     void Start(){
  26.       avatarDriver = VRLibrary.Instance(gameObject).Avatar.GetComponent<AvatarOverrideDriver>();
  27.     }
  28.  
  29.     void LateUpdate(){
  30.         if(grabbedBy != null){
  31.             // If we are currently grabbed, copy position and maybe rotation
  32.             grabbableRB.transform.position = grabbedBy.grabCenter.position;
  33.             if(grabWithRotation){
  34.                 grabbableRB.transform.rotation = grabbedBy.grabCenter.rotation;
  35.             }
  36.         }
  37.     }
  38.  
  39.     public override void Grabbed(Grabber grabber){
  40.         // If something else is currently grabbing this grabbable, let go first
  41.         if(grabbedBy != null){
  42.             grabbedBy.ForceDropObjects();
  43.         }
  44.         // When grabbed, record we were grabbed
  45.         grabbedBy = grabber;
  46.         // Tell the avatar driver to snap the players hand to a point.
  47.         avatarDriver.OverrideHand(snapPoint, grabber.whichHand == Hand.Left, false);
  48.         // Then invoke the event so listening scripts know we were grabbed
  49.         OnGrabbed.Invoke(this);
  50.     }
  51.  
  52.     public override void Ungrabbed(Grabber grabber){
  53.         // When released, record that we were released
  54.         grabbedBy = null;
  55.         // Tell the avatar driver to return to normal functionality
  56.         avatarDriver.OverrideHand(null, grabber.whichHand == Hand.Left);
  57.         // Let other scripts listening to this one know this object was released
  58.         OnUngrabbed.Invoke(this);
  59.  
  60.     }
  61. }
  62.  
Advertisement
Add Comment
Please, Sign In to add comment