Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.Events;
- using Sirenix.OdinInspector;
- // This is a grabbable which snaps the hand of the player to a snapPoint
- public class SnapGrabbable : Grabbable{
- [Tooltip("The grabbable will be treated as if this is its Rigidbody.")]
- [Required, SceneObjectsOnly]
- public Rigidbody grabbableRB;
- [Tooltip("The players hand will snap to this position when this object is grabbed.")]
- [Required, SceneObjectsOnly]
- public Transform snapPoint;
- [Tooltip("If true rotation of the grabbed object will copy rotation of the players hand, otherwise only location will be copied.")]
- public bool grabWithRotation = true;
- // Events that allow other scripts to know when an object is grabbed or released
- public GrabbedEvent OnGrabbed;
- public GrabbedEvent OnUngrabbed;
- private AvatarOverrideDriver avatarDriver;
- void Start(){
- avatarDriver = VRLibrary.Instance(gameObject).Avatar.GetComponent<AvatarOverrideDriver>();
- }
- void LateUpdate(){
- if(grabbedBy != null){
- // If we are currently grabbed, copy position and maybe rotation
- grabbableRB.transform.position = grabbedBy.grabCenter.position;
- if(grabWithRotation){
- grabbableRB.transform.rotation = grabbedBy.grabCenter.rotation;
- }
- }
- }
- public override void Grabbed(Grabber grabber){
- // If something else is currently grabbing this grabbable, let go first
- if(grabbedBy != null){
- grabbedBy.ForceDropObjects();
- }
- // When grabbed, record we were grabbed
- grabbedBy = grabber;
- // Tell the avatar driver to snap the players hand to a point.
- avatarDriver.OverrideHand(snapPoint, grabber.whichHand == Hand.Left, false);
- // Then invoke the event so listening scripts know we were grabbed
- OnGrabbed.Invoke(this);
- }
- public override void Ungrabbed(Grabber grabber){
- // When released, record that we were released
- grabbedBy = null;
- // Tell the avatar driver to return to normal functionality
- avatarDriver.OverrideHand(null, grabber.whichHand == Hand.Left);
- // Let other scripts listening to this one know this object was released
- OnUngrabbed.Invoke(this);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment