RotX18 / MP_Group4_EscapingReality

VR Escape room game developed in Unity
3 stars 0 forks source link

IPickable Interface and Implementation #142

Closed RotX18 closed 2 years ago

RotX18 commented 2 years ago

Interface was actually implemented in #27 on 21/07/2022 but documented there instead of in its own issue

Objective: An interface that allows for a more streamlined way of interacting with grabbable objects within the scene

RotX18 commented 2 years ago

Completed implementation

IPickable interface implementation

IPickable.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public interface IPickable
{
    enum Controller {
        LTouch,
        RTouch,
        None
    }

    bool Grabbed{
        get; 
        set;
    }

    Controller CurrentController {
        get;
        set; 
    }

    void OnRelease();
}

Description:

PickUpBehaviour.cs (Modified Code)

private void Update() {
        //LEFT HAND
        if(OVRInput.Get(OVRInput.RawButton.LHandTrigger)) {
            //if the left hand trigger is pressed more than half, raycast in the +X axis of the controller
            _doLeftRaycast = true;
            if(_leftHandObj != null) {
                //if there is an object below the left hand, set the obj's position to just below the hand
                _leftHandObj.transform.SetParent(leftAnchor.transform);
                _leftHandObj.transform.position = leftAnchor.transform.position;

                //setting the vars if the object implements IPickable
                if(_leftHandObj.GetComponent<IPickable>() != null){
                    _leftHandObj.GetComponent<IPickable>().Grabbed = true;
                    _leftHandObj.GetComponent<IPickable>().CurrentController = IPickable.Controller.LTouch;
                }
            }
        }
        else {
            //When the left hand trigger is let go, reset the vars
            _doLeftRaycast = false;
            try {
                if(_leftHandObj.GetComponent<IPickable>() != null) {
                    _leftHandObj.GetComponent<IPickable>().Grabbed = false;
                    _leftHandObj.GetComponent<IPickable>().CurrentController = IPickable.Controller.None;
                    _leftHandObj.GetComponent<IPickable>().OnRelease();
                }
                _leftHandObj.transform.parent = null;
                _leftHandObj = null;
            }
            catch(System.NullReferenceException) {

            }
        }
}

Edits: