Beispiel #1
0
        //Disable unity physics and collision. For moving item through code
        static public void FreezeItem(GameObject item, bool disableAllColliders = false, bool disableTriggerColliders = false, bool disableNonTriggerColliders = false)
        {
            VRInteractableItem.DisableObjectColliders(item, disableAllColliders, disableTriggerColliders, disableNonTriggerColliders);
            Rigidbody itemBody = item.GetComponent <Rigidbody>();

            if (itemBody != null)
            {
                RigidbodyMarker.ReplaceRigidbodyWithMarker(itemBody);
            }
        }
        //Disable unity physics and collision. For moving item through code
        static public void FreezeItem(GameObject item, bool disableAllColliders = false, bool disableTriggerColliders = false, bool disableNonTriggerColliders = false)
        {
            VRInteractableItem.DisableObjectColliders(item, disableAllColliders, disableTriggerColliders, disableNonTriggerColliders);
            Rigidbody itemBody = item.GetComponentInChildren <Rigidbody>();

            if (itemBody != null)
            {
                itemBody.velocity      = Vector3.zero;
                itemBody.useGravity    = false;
                itemBody.isKinematic   = true;
                itemBody.constraints   = RigidbodyConstraints.FreezeAll;
                itemBody.interpolation = RigidbodyInterpolation.None;
            }
        }
Beispiel #3
0
        virtual public void Drop(bool addControllerVelocity, VRInteractor hand = null)
        {
            if (canBeHeld && item != null)
            {
                item.parent = null;
                switch (getHoldType)
                {
                case HoldType.FIXED_POSITION:
                case HoldType.PICKUP_POSITION:
                    VRInteractableItem.UnFreezeItem(item.gameObject);
                    if (selfBody != null)
                    {
                        if (hand != null && addControllerVelocity)
                        {
                            bool useBoost = hand.Velocity.magnitude > 1f;
                            selfBody.velocity           = hand.Velocity * (useBoost ? getThrowBoost : 1f);
                            selfBody.angularVelocity    = hand.AngularVelocity;
                            selfBody.maxAngularVelocity = selfBody.angularVelocity.magnitude;
                        }
                    }
                    break;

                case HoldType.SPRING_JOINT:
                    for (int i = _heldBys.Count - 1; i >= 0; i--)
                    {
                        if (_heldBys[i] != hand)
                        {
                            continue;
                        }
                        _heldBys.RemoveAt(i);
                        Destroy(_springJoints[i]);
                        _springJoints.RemoveAt(i);
                    }
                    Rigidbody controllerBody = hand.getControllerAnchorOffset.GetComponent <Rigidbody>();
                    if (controllerBody != null)
                    {
                        Destroy(controllerBody);
                    }
                    break;
                }
                PlaySound(dropSound);
            }
            CheckIK(false, hand);
            if (dropEvent != null)
            {
                dropEvent.Invoke();
            }
            heldBy = null;
        }
Beispiel #4
0
 void Start()
 {
     if (target == null)
     {
         Debug.LogError("No Target specified", gameObject);
         return;
     }
     item = GetComponent <VRInteractableItem>();
     if (item == null)
     {
         Debug.LogError("This script requires an VRInteracableItem script on the same object", gameObject);
         return;
     }
     target.gameObject.SetActive(startEnabled);
     _enabled = startEnabled;
 }
        virtual protected bool ItemWithinColliderBounds(VRInteractableItem item)
        {
            if (item.triggerColliders.Count == 0)
            {
                return(false);
            }

            foreach (Collider col in item.triggerColliders)
            {
                //Is the controller anchor offset within the bounds of this collider
                if (col.bounds.Contains(getControllerAnchorOffset.position))
                {
                    return(true);
                }
            }
            return(false);
        }
Beispiel #6
0
        //Enable unity physics
        static public void UnFreezeItem(GameObject item)
        {
            Collider[] itemColliders = item.GetComponentsInChildren <Collider>();
            foreach (Collider col in itemColliders)
            {
                VRInteractableItem ii = null;
                VRItemCollider     ic = col.GetComponent <VRItemCollider>();
                if (ic != null)
                {
                    ii = ic.item;
                }
                if (ii == null)
                {
                    ii = col.GetComponent <VRInteractableItem>();
                }
                if (ii != null && (ii.parents.Count != 0 || !ii.enabled || ii.interactionDisabled))
                {
                    continue;
                }
                col.enabled = true;
            }
            RigidbodyMarker bodyMarker = item.GetComponent <RigidbodyMarker>();

            if (bodyMarker != null)
            {
                Rigidbody body = bodyMarker.ReplaceMarkerWithRigidbody();
                body.isKinematic = false;
            }
            else
            {
                Rigidbody body = item.GetComponent <Rigidbody>();
                if (body != null)
                {
                    body.isKinematic = false;
                }
            }

            /*Rigidbody itemBody = item.GetComponentInChildren<Rigidbody>();
             * if (itemBody != null)
             * {
             *      itemBody.useGravity = true;
             *      itemBody.isKinematic = false;
             *      itemBody.constraints = RigidbodyConstraints.None;
             *      itemBody.interpolation = RigidbodyInterpolation.Interpolate;
             * }*/
        }
        /// <summary>
        /// Calls method on the focus item, either the hover item or held item.
        /// </summary>
        /// <param name="method">Method Name.</param>
        public void SendFocusItemMethod(string method)
        {
            VRInteractableItem item = null;

            if (heldItem != null)
            {
                item = heldItem;
            }
            else if (hoverItem != null)
            {
                item = hoverItem;
            }
            if (item != null && item.CanAcceptMethod(method))
            {
                item.gameObject.SendMessage(method, this, SendMessageOptions.DontRequireReceiver);
            }
        }
        virtual public void Drop()
        {
            if (_heldItem == null || beingDestroyed)
            {
                return;
            }
            VREvent.Send("Drop", new object[] { _heldItem });
            if (hoverItem != null)
            {
                hoverItem.DisableHover(this);
                hoverItem = null;
            }
            _lastDropped = Time.time;
            _heldItem.Drop(true, this);
            _heldItem = null;

            if (hideControllersWhileHolding)
            {
                ToggleControllers(true);
            }
        }
Beispiel #9
0
        virtual protected bool ItemWithinColliderBounds(VRInteractableItem item)
        {
            if (item.triggerColliders.Count == 0)
            {
                return(false);
            }

            foreach (Collider col in item.triggerColliders)
            {
                if (col == null)
                {
                    Debug.LogError("Item has an empty collider in trigger colliders list: " + item.name, item.gameObject);
                    continue;
                }
                //Is the controller anchor offset within the bounds of this collider
                if (col.bounds.Contains(getControllerAnchorOffset.position))
                {
                    return(true);
                }
            }
            return(false);
        }
Beispiel #10
0
        //Set item up to be held correctly
        static public void HeldFreezeItem(GameObject item)
        {
            Collider[] itemColliders = item.GetComponentsInChildren <Collider>();
            foreach (Collider col in itemColliders)
            {
                VRInteractableItem ii = null;
                VRItemCollider     ic = col.GetComponent <VRItemCollider>();
                if (ic != null)
                {
                    ii = ic.item;
                }
                if (ii == null)
                {
                    ii = col.GetComponent <VRInteractableItem>();
                }
                if (ii != null && (ii.parents.Count != 0 || !ii.enabled || ii.interactionDisabled))
                {
                    continue;
                }
                col.enabled = true;
            }
            RigidbodyMarker bodyMarker = item.GetComponent <RigidbodyMarker>();

            if (bodyMarker != null)
            {
                Rigidbody body = bodyMarker.ReplaceMarkerWithRigidbody();
                body.isKinematic = false;
            }
            else
            {
                Rigidbody body = item.GetComponent <Rigidbody>();
                if (body != null)
                {
                    body.isKinematic = false;
                }
            }
        }
        virtual protected void CheckHover()
        {
            if (_heldItem != null)             //If were holding something
            {
                if (_highlighting)             //If were holding something and still highligting then stop
                {
                    highlighting = false;
                }
                return;                 //End of update (We're holding something so no need to carry on)
            }

            VRInteractableItem closestItem = null;
            float closestDist = float.MaxValue;
            bool  forceGrab   = false;

            foreach (VRInteractableItem item in VRInteractableItem.items)
            {
                if (item == null || !item.CanInteract())
                {
                    continue;
                }
                Vector3 controllerPosition = getControllerAnchorOffset.position;
                Vector3 targetPosition     = item.GetWorldHeldPosition(this);
                float   dist = Vector3.Distance(controllerPosition, targetPosition);

                if (dist > closestDist)
                {
                    continue;
                }
                bool canGrab     = false;
                bool isForceGrab = false;
                if (dist < item.interactionDistance || ItemWithinColliderBounds(item))
                {
                    canGrab = true;
                }
                if ((item.interactionDistance < forceGrabDistance &&
                     VRUtils.PositionWithinCone(controllerPosition,
                                                getControllerAnchorOffset.TransformVector(new Vector3(vrInput.LeftHand ? forceGrabDirection.x : -forceGrabDirection.x, forceGrabDirection.y, forceGrabDirection.z)),
                                                targetPosition, 20f, forceGrabDistance)))
                {
                    canGrab     = true;
                    isForceGrab = true;
                }
                if (canGrab)
                {
                    forceGrab   = isForceGrab;
                    closestDist = dist;
                    closestItem = item;
                }
            }

            if (hoverItem != null && hoverItem != closestItem)
            {
                highlighting = false;
                hoverItem.DisableHover(this);
            }
            if (closestItem != null && (hoverItem != closestItem))
            {
                ForceGrabToggle forceToggle        = GetComponent <ForceGrabToggle>();
                bool            forceToggleAllowed = true;
                if (forceToggle != null)
                {
                    forceToggleAllowed = !forceGrab || !vrInput.ActionPressed(forceToggle.actionName);
                }

                if (_lastDropped + 0.5f < Time.time && forceToggleAllowed && (vrInput.ActionPressed("ACTION") || vrInput.ActionPressed("PICKUP_DROP") || vrInput.ActionPressed("PICKUP")))
                {
                    hoverItem = closestItem;
                    string actionDown = "PICKUP";
                    if (vrInput.ActionPressed("PICKUP_DROP"))
                    {
                        actionDown = "PICKUP_DROP";
                    }
                    else if (vrInput.ActionPressed("ACTION"))
                    {
                        actionDown = "ACTION";
                    }
                    SendMessage("InputReceived", actionDown, SendMessageOptions.DontRequireReceiver);
                    return;
                }
                else if (hoverItem != closestItem)
                {
                    highlighting = true;
                    closestItem.EnableHover(this);
                }
            }

            hoverItem = closestItem;
        }
Beispiel #12
0
 virtual public void OnEnable()
 {
     interactableItem = (VRInteractableItem)target;
 }
        void OnGUI()
        {
            var oldInteractableItem = interactableItem;

            interactableItem = (VRInteractableItem)EditorGUILayout.ObjectField("Interactive Item", interactableItem, typeof(VRInteractableItem), true);
            if (interactableItem == null)
            {
                return;
            }
            if (oldInteractableItem != interactableItem || serializedItem == null)
            {
                Init();
            }

            serializedItem.Update();

            SerializedProperty item = serializedItem.FindProperty("item");

            if (item.objectReferenceValue == null)
            {
                item.objectReferenceValue = EditorGUILayout.ObjectField("Item", item.objectReferenceValue, typeof(Transform), true);
                if (item.objectReferenceValue == null)
                {
                    return;
                }
            }

            SerializedProperty linkedLeftAndRightHeldPositions = serializedItem.FindProperty("linkedLeftAndRightHeldPositions");

            linkedLeftAndRightHeldPositions.boolValue = EditorGUILayout.Toggle("Linked Left And Right Held Positions", linkedLeftAndRightHeldPositions.boolValue);



            SerializedProperty heldPosition = serializedItem.FindProperty("heldPosition");

            heldPosition.vector3Value = EditorGUILayout.Vector3Field(linkedLeftAndRightHeldPositions.boolValue ? "Held Position" : "Left Held Position", heldPosition.vector3Value);

            SerializedProperty heldRotation     = serializedItem.FindProperty("heldRotation");
            Quaternion         tempHeldRotation = heldRotation.quaternionValue;

            tempHeldRotation.eulerAngles = EditorGUILayout.Vector3Field(linkedLeftAndRightHeldPositions.boolValue ? "Held Rotation" : "Left Held Rotation", tempHeldRotation.eulerAngles);
            heldRotation.quaternionValue = tempHeldRotation;

            SerializedProperty heldPositionRight     = serializedItem.FindProperty("heldPositionRightHand");
            SerializedProperty heldRotationRightHand = serializedItem.FindProperty("heldRotationRightHand");

            if (!linkedLeftAndRightHeldPositions.boolValue)
            {
                heldPositionRight.vector3Value = EditorGUILayout.Vector3Field("Right Held Position", heldPositionRight.vector3Value);

                Quaternion tempHeldRotationRightHand = heldRotationRightHand.quaternionValue;
                tempHeldRotationRightHand.eulerAngles = EditorGUILayout.Vector3Field("Right Held Rotation", tempHeldRotationRightHand.eulerAngles);
                heldRotationRightHand.quaternionValue = tempHeldRotationRightHand;
            }

            /*SerializedProperty heldPositionOculus = serializedItem.FindProperty("heldPositionOculus");
             * heldPositionOculus.vector3Value = EditorGUILayout.Vector3Field(linkedLeftAndRightHeldPositions.boolValue ? "Held Position Oculus" : "Left Held Position Oculus", heldPositionOculus.vector3Value);
             * SerializedProperty heldRotationOculus = serializedItem.FindProperty("heldRotationOculus");
             * Quaternion tempHeldRotationOculus = heldRotationOculus.quaternionValue;
             * tempHeldRotationOculus.eulerAngles = EditorGUILayout.Vector3Field(linkedLeftAndRightHeldPositions.boolValue ? "Held Rotation Oculus" : "Left Held Rotation Oculus", tempHeldRotationOculus.eulerAngles);
             * heldRotationOculus.quaternionValue = tempHeldRotationOculus;
             *
             * SerializedProperty heldPositionOculusRightHand = serializedItem.FindProperty("heldPositionOculusRightHand");
             * SerializedProperty heldRotationOculusRightHand = serializedItem.FindProperty("heldRotationOculusRightHand");
             * if (!linkedLeftAndRightHeldPositions.boolValue)
             * {
             *      heldPositionOculusRightHand.vector3Value = EditorGUILayout.Vector3Field("Right Held Position Oculus", heldPositionOculusRightHand.vector3Value);
             *
             *      Quaternion tempHeldRotationOculusRightHand = heldRotationOculusRightHand.quaternionValue;
             *      tempHeldRotationOculusRightHand.eulerAngles = EditorGUILayout.Vector3Field("Right Held Rotation Oculus", tempHeldRotationOculusRightHand.eulerAngles);
             *      heldRotationOculusRightHand.quaternionValue = tempHeldRotationOculusRightHand;
             * }*/

            bool updatePrefab = false;

            if (controllerInstance == null)
            {
                bool       makingReferenceController = false;
                GameObject referenceControllerPrefab = null;
                if (GUILayout.Button("Create SteamVR Reference Controller"))
                {
                    makingReferenceController = true;
                    referenceControllerPrefab = Resources.Load <GameObject>("ViveController");
                    if (referenceControllerPrefab == null)
                    {
                        Debug.LogError("Can't find ViveController in resources");
                    }
                }
                if (GUILayout.Button("Create Oculus Reference Controller"))
                {
                    makingReferenceController = true;
                    referenceControllerPrefab = Resources.Load <GameObject>("OculusController");
                    if (referenceControllerPrefab == null)
                    {
                        Debug.LogError("Can't find OculusController in resources");
                    }
                }
                if (makingReferenceController && referenceControllerPrefab != null)
                {
                    leftHand           = true;
                    controllerInstance = (GameObject)Instantiate(referenceControllerPrefab, Vector3.zero, Quaternion.identity);
                    Undo.RegisterCreatedObjectUndo(controllerInstance, "Create Reference Controller");
                    oldPosition = interactableItem.item.position;
                    oldRotation = interactableItem.item.rotation;
                    oldParent   = interactableItem.item.parent;
                    interactableItem.item.SetParent(controllerInstance.transform);
                    interactableItem.item.localPosition = heldPosition.vector3Value;
                    interactableItem.item.localRotation = heldRotation.quaternionValue;
                    Vector3 diff = oldPosition - interactableItem.item.position;
                    controllerInstance.transform.position = diff + heldPosition.vector3Value;
                    controllerInstance.transform.rotation = oldRotation;
                    Selection.activeGameObject            = interactableItem.item.gameObject;
                }
            }
            else
            {
                EditorGUILayout.HelpBox("Make sure to move the target item and not just the object with this script on", MessageType.Info);
                bool saveChange = false;
                if (GUILayout.Button(linkedLeftAndRightHeldPositions.boolValue ? "Save" : "Save Left"))
                {
                    saveChange = true;
                    heldPosition.vector3Value    = interactableItem.item.localPosition;
                    heldRotation.quaternionValue = interactableItem.item.localRotation;
                }
                if (!linkedLeftAndRightHeldPositions.boolValue)
                {
                    if (GUILayout.Button("Save Right"))
                    {
                        saveChange = true;
                        heldPositionRight.vector3Value        = interactableItem.item.localPosition;
                        heldRotationRightHand.quaternionValue = interactableItem.item.localRotation;
                    }
                }
                if (saveChange)
                {
                    Undo.SetTransformParent(interactableItem.item, oldParent, "Save Changes");
                    Undo.RecordObject(interactableItem.item, "Save Changes");
                    interactableItem.item.position = oldPosition;
                    interactableItem.item.rotation = oldRotation;
                    Undo.DestroyObjectImmediate(controllerInstance);
                    if (attachmentInstance != null && gunHandlerWindow)
                    {
                        updatePrefab = true;
                    }
                }

                if (!linkedLeftAndRightHeldPositions.boolValue)
                {
                    if (GUILayout.Button("Toggle " + (leftHand ? "Right" : "Left") + " Hand Position"))
                    {
                        leftHand = !leftHand;
                        if (leftHand)
                        {
                            interactableItem.item.localPosition = heldPosition.vector3Value;
                            interactableItem.item.localRotation = heldRotation.quaternionValue;
                        }
                        else
                        {
                            interactableItem.item.localPosition = heldPositionRight.vector3Value;
                            interactableItem.item.localRotation = heldRotationRightHand.quaternionValue;
                        }
                    }
                }

                if (GUILayout.Button("Select Controller"))
                {
                    Selection.activeGameObject = controllerInstance;
                }
                if (GUILayout.Button("Select Item"))
                {
                    Selection.activeGameObject = interactableItem.item.gameObject;
                }
                if (GUILayout.Button("Cancel"))
                {
                    Undo.SetTransformParent(interactableItem.item, oldParent, "Cancel");
                    Undo.RecordObject(interactableItem.item, "Save Changes");
                    interactableItem.item.position = oldPosition;
                    interactableItem.item.rotation = oldRotation;
                    Undo.DestroyObjectImmediate(controllerInstance);
                }
            }
            serializedItem.ApplyModifiedProperties();

            EditorGUILayout.HelpBox("You can set up the held position using either the SteamVR Vive Controller or" +
                                    " the Native Oculus Controller. The two systems use a different base rotation so you can either base" +
                                    " the held position on steamVR or Oculus, so choose which one then set up all of your held positions with" +
                                    " that controller. For the other controller you can use the 'Controller Rotation Offset' value on the interactor" +
                                    " script attached to each controller to offset for either the alternative controller or to offset an alternative " +
                                    "anchor in the case of an IK hand for when you're not using the controller as the anchor.", MessageType.Info);


            if (updatePrefab && attachmentInstance != null && gunHandlerWindow && OnSaveEvent != null)
            {
                OnSaveEvent(this);
            }
        }
Beispiel #14
0
        virtual protected void OnGUI()
        {
            GUILayout.Label("Welcome To The FinalIK hand poser for VRInteraction items", EditorStyles.boldLabel);
            EditorGUILayout.HelpBox("Reference the VRIK component of the target character you want to set hand poses for, then reference " +
                                    "the item you want the hand pose for.", MessageType.Info);
            var oldVRIK = _vrIK;

            _vrIK = (VRIK)EditorGUILayout.ObjectField("VRIK", _vrIK, typeof(VRIK), true);
            if (oldVRIK != null && _vrIK != oldVRIK)
            {
                ResetHands(oldVRIK);
                _handPoseController = null;
            }
            _item = (VRInteractableItem)EditorGUILayout.ObjectField("VR Interactable Item", _item, typeof(VRInteractableItem), true);

            if (_vrIK == null || _item == null || _item.item == null || IsPrefab(_item.item.gameObject))
            {
                return;
            }

            if (_handPoseController == null)
            {
                _handPoseController = _vrIK.GetComponent <HandPoseController>();
                if (_handPoseController == null)
                {
                    _handPoseController = _vrIK.gameObject.AddComponent <HandPoseController>();
                }

                //Set pose name to default if set
                foreach (Transform pose in _handPoseController.poses)
                {
                    if (pose.name == _item.leftHandIKPoseName)
                    {
                        _leftPoseName = pose.name;
                    }
                    if (pose.name == _item.rightHandIkPoseName)
                    {
                        _rightPoseName = pose.name;
                    }
                }
            }

            GUILayout.BeginHorizontal();
            switch (_hand)
            {
            case Hand.LEFT:
                GUILayout.Box("Left Hand", GUILayout.ExpandWidth(true));
                if (GUILayout.Button("Right Hand"))
                {
                    _hand = Hand.RIGHT;
                }
                break;

            case Hand.RIGHT:
                if (GUILayout.Button("Left Hand"))
                {
                    _hand = Hand.LEFT;
                }
                GUILayout.Box("Right Hand", GUILayout.ExpandWidth(true));
                break;
            }
            GUILayout.EndHorizontal();

            Transform handTrans = _hand == Hand.LEFT ? _vrIK.references.leftHand : _vrIK.references.rightHand;

            if (handTrans == null)
            {
                EditorGUILayout.HelpBox("VRIK left or right hand reference is null", MessageType.Error);
                return;
            }

            DefaultPoses();

            bool changed = ShowExistingPoses();

            if (changed)
            {
                if (_hand == Hand.LEFT && _leftPoseIndex < _handPoseController.poses.Count &&
                    _handPoseController.poses[_leftPoseIndex] != null)
                {
                    _leftPoseName = _handPoseController.poses[_leftPoseIndex].name;
                }
                else if (_rightPoseIndex < _handPoseController.poses.Count &&
                         _handPoseController.poses[_rightPoseIndex] != null)
                {
                    _rightPoseName = _handPoseController.poses[_rightPoseIndex].name;
                }
            }

            if (_hand == Hand.LEFT)
            {
                _leftPoseName = EditorGUILayout.TextField("Pose Name", _leftPoseName);
            }
            else
            {
                _rightPoseName = EditorGUILayout.TextField("Pose Name", _rightPoseName);
            }
            if (GUILayout.Button("Move Hand To Item"))
            {
                MoveToItem();
            }
            if (GUILayout.Button("Reset Hand"))
            {
                ResetHands(_vrIK);
            }
            EditorGUI.BeginDisabledGroup((_hand == Hand.LEFT ? _vrIK.references.leftHand : _vrIK.references.rightHand) == null ||
                                         string.IsNullOrEmpty(_hand == Hand.LEFT ? _leftPoseName : _rightPoseName));
            if (GUILayout.Button("Select Hand Object"))
            {
                Selection.activeGameObject = _hand == Hand.LEFT ? _vrIK.references.leftHand.gameObject : _vrIK.references.rightHand.gameObject;
            }
            if (GUILayout.Button("Save Pose"))
            {
                SaveHand();
            }
            EditorGUI.EndDisabledGroup();
        }
        virtual public void Drop(bool addControllerVelocity, VRInteractor hand = null)
        {
            if (canBeHeld && item != null)
            {
                if (hand != null)
                {
                    NetworkIdentity ident = item.GetComponent <NetworkIdentity>();
                    if (ident != null)
                    {
                        NetworkedCameraRig networkedRig = hand.GetVRRigRoot.GetComponent <NetworkedCameraRig>();
                        if (networkedRig != null && networkedRig.connection != null && networkedRig.connection.isLocalPlayer)
                        {
                            ident.RemoveClientAuthority(networkedRig.connection.connectionToClient);
                        }
                    }
                }

                item.parent = null;
                switch (holdType)
                {
                case HoldType.FIXED_POSITION:
                case HoldType.PICKUP_POSITION:
                    VRInteractableItem.UnFreezeItem(item.gameObject);
                    if (_selfBody != null && addControllerVelocity)
                    {
                        if (hand != null)
                        {
                            bool useBoost = hand.Velocity.magnitude > 1f;
                            _selfBody.velocity           = hand.Velocity * (useBoost ? throwBoost : 1f);
                            _selfBody.angularVelocity    = hand.AngularVelocity;
                            _selfBody.maxAngularVelocity = _selfBody.angularVelocity.magnitude;
                        }
                    }
                    break;

                case HoldType.SPRING_JOINT:
                    for (int i = _heldBys.Count - 1; i >= 0; i--)
                    {
                        if (_heldBys[i] != hand)
                        {
                            continue;
                        }
                        _heldBys.RemoveAt(i);
                        Destroy(_springJoints[i]);
                        _springJoints.RemoveAt(i);
                    }
                    Rigidbody controllerBody = hand.getControllerAnchorOffset.GetComponent <Rigidbody>();
                    if (controllerBody != null)
                    {
                        Destroy(controllerBody);
                    }
                    break;
                }
                PlaySound(dropSound);
            }
            CheckIK(false, hand);
            if (dropEvent != null)
            {
                dropEvent.Invoke();
            }
            heldBy = null;
        }
        virtual public bool Pickup(VRInteractor hand)
        {
            if (canBeHeld && item != null)
            {
                NetworkIdentity ident = item.GetComponent <NetworkIdentity>();
                if (ident != null)
                {
                    NetworkedCameraRig networkedRig = hand.GetVRRigRoot.GetComponent <NetworkedCameraRig>();
                    if (networkedRig != null && networkedRig.connection != null && networkedRig.connection.isLocalPlayer)
                    {
                        ident.AssignClientAuthority(networkedRig.connection.connectionToClient);
                    }
                }

                switch (holdType)
                {
                case HoldType.FIXED_POSITION:
                    item.SetParent(hand.GetVRRigRoot);
                    StartCoroutine(PickingUp(hand));
                    VRInteractableItem.HeldFreezeItem(item.gameObject);
                    break;

                case HoldType.PICKUP_POSITION:
                    if (Vector3.Distance(hand.getControllerAnchorOffset.position, item.position) < interactionDistance)
                    {
                        heldPosition = hand.getControllerAnchorOffset.InverseTransformPoint(item.position);
                    }
                    else
                    {
                        heldPosition = Vector3.zero;
                    }
                    heldRotation = Quaternion.Inverse(hand.getControllerAnchorOffset.rotation) * item.rotation;
                    item.SetParent(hand.GetVRRigRoot);
                    StartCoroutine(PickingUp(hand));
                    VRInteractableItem.HeldFreezeItem(item.gameObject);
                    break;

                case HoldType.SPRING_JOINT:
                    SpringJoint springJoint    = item.gameObject.AddComponent <SpringJoint>();
                    Rigidbody   controllerBody = hand.getControllerAnchorOffset.GetComponent <Rigidbody>();
                    if (controllerBody == null)
                    {
                        controllerBody = hand.getControllerAnchorOffset.gameObject.AddComponent <Rigidbody>();
                    }
                    controllerBody.isKinematic = true;
                    controllerBody.useGravity  = false;
                    springJoint.connectedBody  = controllerBody;
                    //springJoint.anchor = Vector3.zero;
                    springJoint.anchor = item.InverseTransformPoint(hand.getControllerAnchorOffset.position);
                    springJoint.autoConfigureConnectedAnchor = false;
                    springJoint.connectedAnchor = Vector3.zero;
                    springJoint.spring          = followForce * 100f;
                    springJoint.damper          = 100f;
                    _springJoints.Add(springJoint);
                    _heldBys.Add(hand);
                    break;
                }

                if (Vector3.Distance(hand.getControllerAnchorOffset.position, item.position) < interactionDistance)
                {
                    PlaySound(pickupSound);
                }
                else
                {
                    PlaySound(forceGrabSound, hand.getControllerAnchorOffset.position);
                }
            }
            if (pickupEvent != null)
            {
                pickupEvent.Invoke();
            }
            CheckIK(true, hand);
            heldBy = hand;
            return(true);
        }
Beispiel #17
0
        public override void OnInspectorGUI()
        {
            serializedObject.Update();

            SerializedProperty controllerAnchor        = serializedObject.FindProperty("controllerAnchor");
            SerializedProperty controllerAnchorOffset  = serializedObject.FindProperty("controllerAnchorOffset");
            GUIContent         controllerAnchorContent = new GUIContent("Controller Anchor", "Reference the hand of a IK model if you are using one, otherwise leave blank to use the controller");

            if (controllerAnchor.objectReferenceValue == null)
            {
                controllerAnchor.objectReferenceValue = interactor.transform;
            }
            var oldControllerAnchor = controllerAnchor.objectReferenceValue;

            EditorGUILayout.PropertyField(controllerAnchor, controllerAnchorContent);
            if (oldControllerAnchor != controllerAnchor.objectReferenceValue)
            {
                if (controllerAnchorOffset.objectReferenceValue != null)
                {
                    DestroyImmediate(((Transform)controllerAnchorOffset.objectReferenceValue).gameObject);
                }
                controllerAnchorOffset.objectReferenceValue = null;
            }

            if (controllerAnchorOffset.objectReferenceValue == null)
            {
                Transform newAnchorOffset = null;
                foreach (Transform trans in (Transform)controllerAnchor.objectReferenceValue)
                {
                    if (trans.name == VRInteractor.anchorOffsetName)
                    {
                        newAnchorOffset = trans;
                        break;
                    }
                }
                if (newAnchorOffset == null)
                {
                    interactor.controllerAnchor = (Transform)controllerAnchor.objectReferenceValue;
                }
                controllerAnchorOffset.objectReferenceValue = newAnchorOffset == null ? interactor.getControllerAnchorOffset : newAnchorOffset;
            }
            EditorGUILayout.PropertyField(controllerAnchorOffset);

            SerializedProperty ikTarget        = serializedObject.FindProperty("ikTarget");
            GUIContent         ikTargetContent = new GUIContent("IK Target", "If using an IK rig this should be the hand target the rig is pointing to." +
                                                                " Either this object or a child transform");

            EditorGUILayout.PropertyField(ikTarget, ikTargetContent);

            SerializedProperty referenceObject  = serializedObject.FindProperty("objectReference");
            GUIContent         referenceContent = new GUIContent("Object Reference", "The object reference will be set to held at the start. You can use this if you want to fix a gun to the player that can't be dropped");

            referenceObject.objectReferenceValue = EditorGUILayout.ObjectField(referenceContent, referenceObject.objectReferenceValue, typeof(GameObject), true);
            if (badObjectReference)
            {
                EditorGUILayout.HelpBox("Object reference must be an instance in the scene and must have a VRInteractableItem (or something that inherits) script attached", MessageType.Warning);
            }
            if (referenceObject.objectReferenceValue != null)
            {
                SerializedProperty objectReferenceIsPrefab = serializedObject.FindProperty("objectReferenceIsPrefab");
                badObjectReference = false;
                PrefabType         objectPrefabType = PrefabUtility.GetPrefabType(referenceObject.objectReferenceValue);
                VRInteractableItem interactableItem = ((GameObject)referenceObject.objectReferenceValue).GetComponentInChildren <VRInteractableItem>();
                if (interactableItem == null)
                {
                    badObjectReference = true;
                    referenceObject.objectReferenceValue = null;
                }
                objectReferenceIsPrefab.boolValue = objectPrefabType == PrefabType.ModelPrefab || objectPrefabType == PrefabType.Prefab;
            }

            SerializedProperty useHoverLine = serializedObject.FindProperty("useHoverLine");

            EditorGUILayout.PropertyField(useHoverLine);
            if (useHoverLine.boolValue)
            {
                SerializedProperty hoverLineMat = serializedObject.FindProperty("hoverLineMat");
                EditorGUILayout.PropertyField(hoverLineMat);
            }

            SerializedProperty hideControllersWhileHolding = serializedObject.FindProperty("hideControllersWhileHolding");
            GUIContent         hideControllerContent       = new GUIContent("Held Hide Controller", "Hide Controllers While Holding Item");

            hideControllersWhileHolding.boolValue = EditorGUILayout.Toggle(hideControllerContent, hideControllersWhileHolding.boolValue);

            SerializedProperty forceGrabDirection        = serializedObject.FindProperty("forceGrabDirection");
            GUIContent         forceGrabDirectionContent = new GUIContent("Force Grab Direction", "Local Controller Direction, use (1,0,0) for palm or (0,0,1) for forward");

            EditorGUILayout.PropertyField(forceGrabDirection, forceGrabDirectionContent);

            SerializedProperty forceGrabDistance = serializedObject.FindProperty("forceGrabDistance");

            EditorGUILayout.PropertyField(forceGrabDistance);

            serializedObject.ApplyModifiedProperties();

            EditorGUILayout.HelpBox(
                "The VRInteractor script implements the 'Input Received' method called by VRInput, " +
                "it then calls the method matching the action name to any hovered or held VRInteractableItem.\n" +
                "By default an item can respond to:\n" +
                "PICKUP_DROP: Can Pickup a hovered item or drop the currently held item\n" +
                "PICKUP: Can Pickup a hovered item\n" +
                "DROP: Drops the currently held item\n" +
                "ACTION: Calls PICKUP if no item is held (The BasicToggle script uses this to toggle the light on and off)", MessageType.Info);
        }
        /// <summary>
        /// Get item held position in parent tranform local space.
        /// Used on the gun slide to get the controller position as a child of the gunhandler item transform
        /// </summary>
        /// <returns>The local controller position to parent transform.</returns>
        /// <param name="hand">Hand.</param>
        /// <param name="item">Item.</param>
        /// <param name="parent">Parent.</param>
        public static Vector3 GetLocalControllerPositionToParentTransform(VRInteractor hand, VRInteractableItem item, Transform parent)
        {
            Vector3 controllerPosition = item.GetControllerPosition(hand);

            return(parent.InverseTransformPoint(controllerPosition));
        }