virtual protected void CheckIK(bool pickingUp, VRInteractor hand)
 {
     if (hand == null || hand.ikTarget == null)
     {
         return;
     }
     if (pickingUp)
     {
         Transform handIKAnchor = hand.vrInput.LeftHand ? leftHandIKAnchor : rightHandIKAnchor;
         if (handIKAnchor != null)
         {
             hand.SetIKTarget(handIKAnchor);
         }
         if ((hand.vrInput.LeftHand && leftHandIKPoseName != "") ||
             (!hand.vrInput.LeftHand && rightHandIkPoseName != ""))
         {
             //	Method is in HandPoseController.cs, found in the FinalIK integrations folder (make sure to open the FinalIK package in VRInteraction first).
             hand.GetVRRigRoot.BroadcastMessage(hand.vrInput.LeftHand ? "ApplyPoseLeftHand" : "ApplyPoseRightHand", hand.vrInput.LeftHand ? leftHandIKPoseName : rightHandIkPoseName, SendMessageOptions.DontRequireReceiver);
         }
     }
     else
     {
         hand.SetIKTarget(null);
         if ((hand.vrInput.LeftHand && leftHandIKPoseName != "") ||
             (!hand.vrInput.LeftHand && rightHandIkPoseName != ""))
         {
             //	Method is in HandPoseController.cs, found in the FinalIK integrations folder (make sure to open the FinalIK package in VRInteraction first).
             hand.GetVRRigRoot.BroadcastMessage("ClearPose", hand.vrInput.LeftHand, SendMessageOptions.DontRequireReceiver);
         }
     }
 }
        virtual public void DisableHover(VRInteractor hand = null)
        {
            if (!activeHover || interactionDisabled)
            {
                return;
            }
            activeHover = false;
            PlaySound(exitHover);
            if (hovers.Count == 0)
            {
                return;
            }
            for (int i = 0; i < hovers.Count; i++)
            {
                Renderer hover = hovers[i];
                if (hover == null)
                {
                    continue;
                }
                switch (hoverModes[i])
                {
                case HoverMode.SHADER:
                    if (hover.material != null)
                    {
                        hover.material.shader = defaultShaders[i];
                    }
                    break;

                case HoverMode.MATERIAL:
                    hover.material = defaultMats[i];
                    break;
                }
            }
        }
        virtual protected void ACTION(VRInteractor hand)
        {
            if (hand.heldItem != null)
            {
                return;
            }

            PICKUP_DROP(hand);
        }
Ejemplo n.º 4
0
 void ACTION(VRInteractor hand)
 {
     if (target == null || item == null || hand.heldItem != item)
     {
         return;
     }
     _enabled = !_enabled;
     target.gameObject.SetActive(_enabled);
 }
        virtual protected void DROP(VRInteractor hand)
        {
            if (hand.heldItem == null)
            {
                return;
            }

            hand.Drop();
        }
        virtual protected void PICKUP_DROPReleased(VRInteractor hand)
        {
            if (hand.heldItem == null || toggleToPickup || hand.vrInput.ActionPressed("PICKUP") || hand.vrInput.ActionPressed("ACTION") || hand.vrInput.ActionPressed("PICKUP_DROP"))
            {
                return;
            }

            hand.Drop();
        }
        virtual protected void PICKUP(VRInteractor hand)
        {
            if (hand.heldItem != null)
            {
                return;
            }

            hand.TryPickup();
        }
Ejemplo n.º 8
0
 void Update()
 {
     if (vrInteractor != null && vrInteractor.forceGrabDistance == (forceGrabOn ? defaultForceDistance : 0f))
     {
         return;
     }
     vrInteractor = GetComponent <VRInteractor>();
     vrInteractor.forceGrabDistance = forceGrabOn ? defaultForceDistance : 0f;
 }
Ejemplo n.º 9
0
 virtual public void OnEnable()
 {
     interactor = (VRInteractor)target;
     if (interactor.GetComponent <VRInput>() == null)
     {
         interactor.gameObject.AddComponent <VRInput>();
         EditorUtility.SetDirty(interactor);
         EditorSceneManager.MarkSceneDirty(SceneManager.GetActiveScene());
     }
 }
 virtual protected void PICKUP_DROP(VRInteractor hand)
 {
     if (hand.heldItem == null)
     {
         hand.TryPickup();
     }
     else if (toggleToPickup)
     {
         hand.Drop();
     }
 }
        /*public Vector3 ControllerPositionToLocal()
         * {
         *      if (heldBy == null || currentGun == null) return Vector3.zero;
         *      Vector3 localPosition =  currentGun.item.InverseTransformPoint(heldBy.getControllerAnchorOffset.position);
         *      Vector3 rotatedOffset = Quaternion.Inverse(GetLocalHeldRotation(heldBy)) * defaultRotation * GetLocalHeldPosition(heldBy);
         *      Vector3 scaledOffset = new Vector3(rotatedOffset.x/currentGun.item.localScale.x, rotatedOffset.y/currentGun.item.localScale.y, rotatedOffset.z/currentGun.item.localScale.z);
         *      return localPosition + scaledOffset;
         * }*/

        //virtual public Vector3 GetControllerPositionItemAligned(VRInteractor hand)
        //{
        //	return VRUtils.TransformPoint(hand.getControllerAnchorOffset.position, item.rotation, hand.getControllerAnchorOffset.lossyScale, GetLocalHeldPosition(hand));
        //}

        virtual public Vector3 GetLocalHeldPosition(VRInteractor hand)
        {
            if (linkedLeftAndRightHeldPositions || hand.vrInput.LeftHand)
            {
                return(heldPosition);
            }
            else if (!linkedLeftAndRightHeldPositions && !hand.vrInput.LeftHand)
            {
                return(heldPositionRightHand);
            }

            Debug.LogError("No held position. LinkedLeftAndRightHeldPositions: " + linkedLeftAndRightHeldPositions + " hand.LeftHand: " + hand.vrInput.LeftHand);
            return(Vector3.zero);
        }
        virtual public Quaternion GetLocalHeldRotation(VRInteractor hand)
        {
            if (linkedLeftAndRightHeldPositions || hand.vrInput.LeftHand)
            {
                return(heldRotation);
            }
            else if (!linkedLeftAndRightHeldPositions && !hand.vrInput.LeftHand)
            {
                return(heldRotationRightHand);
            }

            Debug.LogError("No held rotation. LinkedLeftAndRightHeldPositions: " + linkedLeftAndRightHeldPositions + " hand.LeftHand: " + hand.vrInput.LeftHand);
            return(Quaternion.identity);
        }
Ejemplo n.º 13
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;
        }
        /// <summary>
        /// Get item held position in world space
        /// </summary>
        /// <returns>The world held position.</returns>
        /// <param name="hand">Hand.</param>
        virtual public Vector3 GetWorldHeldPosition(VRInteractor hand)
        {
            if (item == null)
            {
                return(Vector3.zero);
            }
            switch (holdType)
            {
            case HoldType.FIXED_POSITION:
                return(item.position - (item.rotation * (Quaternion.Inverse(GetLocalHeldRotation(hand)) * GetLocalHeldPosition(hand))));

            case HoldType.PICKUP_POSITION:
            case HoldType.SPRING_JOINT:
                return(item.position);
            }
            return(Vector3.zero);
        }
Ejemplo n.º 15
0
        virtual protected IEnumerator PickingUp(VRInteractor heldBy)
        {
            currentFollowForce = 0.05f;
            _pickingUp         = true;
            float baseDist = Vector3.Distance(GetControllerPosition(heldBy), item.position);

            if (baseDist < 0.1f)
            {
                baseDist = 0.1f;
            }
            float elapsedTime = 0;

            while (currentFollowForce < 0.99f)
            {
                elapsedTime += Time.deltaTime;
                if (elapsedTime > baseDist * 0.25f)
                {
                    break;                                               //Maximum time safety
                }
                float dist    = Vector3.Distance(GetControllerPosition(heldBy), item.position);
                float percent = -((dist / baseDist) - 1f);
                if (baseDist > 1f && percent < 0.3f)
                {
                    percent *= 0.2f;
                }
                if (percent < 0.05f)
                {
                    percent = 0.05f;
                }
                currentFollowForce = getFollowForce * percent;
                yield return(null);

                if (this.heldBy != heldBy)
                {
                    currentFollowForce = getFollowForce;
                    _pickingUp         = false;
                    break;
                }
            }
            CheckIK(true, heldBy);
            _pickingUp         = false;
            currentFollowForce = getFollowForce;
        }
Ejemplo n.º 16
0
 virtual protected void CheckIK(bool pickingUp, VRInteractor hand)
 {
     if (hand == null || hand.ikTarget == null)
     {
         return;
     }
     if (pickingUp)
     {
         Transform handIKAnchor = hand.vrInput.LeftHand ? leftHandIKAnchor : rightHandIKAnchor;
         if (handIKAnchor != null)
         {
             hand.SetIKTarget(handIKAnchor);
         }
     }
     else
     {
         hand.SetIKTarget(null);
     }
 }
Ejemplo n.º 17
0
        virtual public void EnableHover(VRInteractor hand = null)
        {
            if (activeHover || interactionDisabled)
            {
                return;
            }
            activeHover = true;
            PlaySound(enterHover);
            if (enableHoverEvent != null)
            {
                enableHoverEvent.Invoke();
            }
            if (hovers.Count == 0)
            {
                return;
            }
            for (int i = 0; i < hovers.Count; i++)
            {
                Renderer hover = hovers[i];
                if (hover == null)
                {
                    continue;
                }
                switch (hoverModes[i])
                {
                case HoverMode.SHADER:
                    if (hover.material != null)
                    {
                        hover.material.shader = hoverShaders[i];
                    }
                    break;

                case HoverMode.MATERIAL:
                    hover.material = hoverMats[i];
                    break;
                }
            }
        }
Ejemplo n.º 18
0
        public void InputReceived(string method)
        {
            if (vrInteractor == null)
            {
                vrInteractor = GetComponent <VRInteractor>();
                if (vrInteractor == null)
                {
                    Debug.LogError("This script should be on the same object as a VRInteractor");
                    return;
                }
            }

            if (method == actionName)
            {
                forceGrabOn = true;
                vrInteractor.forceGrabDistance = defaultForceDistance;
            }
            else if (method == actionName + "Released")
            {
                forceGrabOn = false;
                vrInteractor.forceGrabDistance = 0f;
            }
        }
        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);
        }
 virtual protected void DROPReleased(VRInteractor hand)
 {
 }
        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 protected void ACTIONReleased(VRInteractor hand)
 {
     PICKUP_DROPReleased(hand);
 }
        /// <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));
        }
Ejemplo n.º 24
0
        void Update()
        {
            if (_input == null)
            {
                _input = GetComponent <VRInput>();
            }
            if (_interactor == null)
            {
                _interactor = GetComponent <VRInteractor>();
            }

            bool inputIsDirty = false;

            if (_triggerPressed != _input.TriggerPressed)
            {
                inputIsDirty = true; _triggerPressed = _input.TriggerPressed;
            }
            if (_triggerPressure != _input.TriggerPressure)
            {
                inputIsDirty = true; _triggerPressure = _input.TriggerPressure;
            }

            if (_padTopPressed != _input.PadTopPressed)
            {
                inputIsDirty = true; _padTopPressed = _input.PadTopPressed;
            }
            if (_padLeftPressed != _input.PadLeftPressed)
            {
                inputIsDirty = true; _padLeftPressed = _input.PadLeftPressed;
            }
            if (_padRightPressed != _input.PadRightPressed)
            {
                inputIsDirty = true; _padRightPressed = _input.PadRightPressed;
            }
            if (_padBottomPressed != _input.PadBottomPressed)
            {
                inputIsDirty = true; _padBottomPressed = _input.PadBottomPressed;
            }
            if (_padCentrePressed != _input.PadCentrePressed)
            {
                inputIsDirty = true; _padCentrePressed = _input.PadCentrePressed;
            }
            if (_padTouched != _input.PadTouched)
            {
                inputIsDirty = true; _padTouched = _input.PadTouched;
            }
            if (_padPressed != _input.PadPressed)
            {
                inputIsDirty = true; _padPressed = _input.PadPressed;
            }
            if (_padPosition != _input.PadPosition)
            {
                inputIsDirty = true; _padPosition = _input.PadPosition;
            }
            if (_gripPressed != _input.GripPressed)
            {
                inputIsDirty = true; _gripPressed = _input.GripPressed;
            }
            if (_menuPressed != _input.MenuPressed)
            {
                inputIsDirty = true; _menuPressed = _input.MenuPressed;
            }
            if (_axPressed != _input.AXPressed)
            {
                inputIsDirty = true; _axPressed = _input.AXPressed;
            }

            bool interactorIsDirty = false;

            if (_velocity != _interactor.Velocity)
            {
                interactorIsDirty = true; _velocity = _interactor.Velocity;
            }
            if (_angularVelocity != _interactor.AngularVelocity)
            {
                interactorIsDirty = true; _angularVelocity = _interactor.AngularVelocity;
            }

            if (inputIsDirty)
            {
                networkRig.InputDirty(_input);
            }
            if (interactorIsDirty)
            {
                networkRig.InteractorDirty(_interactor, _input.LeftHand);
            }
        }
 virtual public Quaternion GetControllerRotation(VRInteractor hand)
 {
     return(heldBy.getControllerAnchorOffset.rotation * GetLocalHeldRotation(heldBy));
 }
 virtual public Vector3 GetControllerPosition(VRInteractor hand)
 {
     return(hand.getControllerAnchorOffset.TransformPoint(GetLocalHeldPosition(hand)));
 }