Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using Sirenix.OdinInspector;
- [RequireComponent(typeof(Rigidbody))]
- public class Grabber : MonoBehaviour
- {
- [Tooltip("The point at the center of the grabbing")]
- [Required, SceneObjectsOnly]
- public Transform grabCenter;
- [Tooltip("Left or Right hand")]
- public Hand whichHand;
- // Horrible no good very bad way to do this, this is currently only referenced in Grabber.cs, MainMenu.cs, and InGameMenu.cs
- public static bool isToggleGrip = true;
- private bool wasToggleGrip = true;
- // The list of all objects the hand could grab if it tried to grab right now
- private HashSet<Grabbable> objectsCanGrab = new HashSet<Grabbable>();
- // The currently grabbed object
- private Grabbable grabbed = null;
- // Tracks whether the grab button is down or not
- private bool isGrabButtonDown = false;
- void Start(){
- // Register the input
- //string inputID = whichHand == Hand.Left ? "Oculus_CrossPlatform_PrimaryHandTrigger" : "Oculus_CrossPlatform_SecondaryHandTrigger";
- string inputID = whichHand == Hand.Left ? ControlManager.Instance(gameObject).NamedInputs["LeftGrab"] : ControlManager.Instance(gameObject).NamedInputs["RightGrab"];
- ControlManager.Instance(gameObject).Inputs[inputID].onPress.AddListener(HandleGrabPressed);
- ControlManager.Instance(gameObject).Inputs[inputID].onRelease.AddListener(HandleGrabReleased);
- }
- void FixedUpdate(){
- if(isToggleGrip != wasToggleGrip){
- isGrabButtonDown = false;
- Ungrab();
- }
- // If the grab button is down and we haven't grabbed anything, keep trying until we let go or grab something
- if(isGrabButtonDown && grabbed == null){
- TryGrab();
- }
- wasToggleGrip = isToggleGrip;
- }
- // Keep track of grabbable objects
- void OnTriggerEnter(Collider other){
- Grabbable potentialGrabbable = other.GetComponentInParent<Grabbable>();
- if(potentialGrabbable != null){
- objectsCanGrab.Add(potentialGrabbable);
- }
- }
- // Deregister grabbable objects
- void OnTriggerExit(Collider other){
- Grabbable potentialGrabbable = other.GetComponentInParent<Grabbable>();
- if(potentialGrabbable != null){
- objectsCanGrab.Remove(potentialGrabbable);
- }
- }
- // 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.
- public void ForceDropObjects(){
- Ungrab();
- }
- // Finds which object we grab when we try to
- private Grabbable GetObjToGrab(){
- Grabbable currentBest = null;
- foreach(Grabbable current in objectsCanGrab){
- if(currentBest == null){
- // First make sure that if there's any object possible to grab, we get at least one
- currentBest = current;
- }else if(current.priorityLevel > currentBest.priorityLevel){
- // Then get objects with higher priority level
- // This lets us label some objects as core to mechanics, thus making it easier to grab them vs less important objects
- currentBest = current;
- }else if((current.location.position - grabCenter.position).magnitude < (currentBest.location.position - grabCenter.position).magnitude){
- // Finally grab the object which the grabber is closes to the center of
- currentBest = current;
- }
- }
- return currentBest;
- }
- // This is called when the grab button is pressed
- private void HandleGrabPressed(ControlManager.AxisInput input){
- if(!isToggleGrip)
- {
- isGrabButtonDown = true;
- TryGrab();
- }else{
- isGrabButtonDown = !isGrabButtonDown;
- if(isGrabButtonDown)
- {
- TryGrab();
- }else{
- Ungrab();
- }
- }
- }
- // This is called when the grab button is pressed
- private void HandleGrabReleased(ControlManager.AxisInput input){
- if(!isToggleGrip)
- {
- isGrabButtonDown = false;
- Ungrab();
- }else{
- // Do Nothing
- }
- }
- // Tries to grab an object, returns whether it succeeded
- private bool TryGrab(){
- grabbed = GetObjToGrab();
- if(grabbed != null){
- grabbed.Grabbed(this);
- return true;
- }else{
- return false;
- }
- }
- // Lets go of an object
- private void Ungrab(){
- if(grabbed != null){
- grabbed.Ungrabbed(this);
- grabbed = null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment