Example #1
0
    // Update is called once per frame
    void Update()
    {
        var rightTrigger = _rightController.GetAxis(EVRButtonId.k_EButton_SteamVR_Trigger).x;
        var rightPos     = _rightController.transform.pos;
        var leftPos      = _thisTransform.position;
        var dir          = leftPos - rightPos;

        dir.Normalize();
        var dist = Vector3.Distance(arrowAnchor.transform.position, rightPos);

        Debug.DrawRay(rightPos, dir);
        _drawProgress = dist / maxDrawDistance;
        _drawProgress = (_drawProgress > 1) ? 1 : (_drawProgress < 0) ? 0 : _drawProgress;
        if (rightTrigger >= 0.2f && _drawProgress <= minDrawDistance && !_isDrawing)
        {
            _isDrawing = true;
        }
        if (_isDrawing)
        {
            if (_notchedArrow == null)
            {
                var g = Instantiate(arrow, _thisTransform);
                g.localPosition = arrowAnchor.localPosition;
                g.localRotation = Quaternion.identity;
                _notchedArrow   = g.GetComponent <ArrowController>();
            }

            effect.SetDrawProgress(_drawProgress);
            _notchedArrow.SetDrawProgress(_drawProgress, rightPos);
            var intensity = (ushort)(drawHapticCurve.Evaluate(_drawProgress) * drawHapticIntensity);
            _thisTransform.rotation = Quaternion.LookRotation(dir, Vector3.up);
            if (_drawProgress >= drawHapticFequency + _lastDrawHaptic || _drawProgress <= _lastDrawHaptic - drawHapticFequency)
            {
                _rightController.TriggerHapticPulse(intensity);
                _lastDrawHaptic = (drawHapticFequency * (int)(_drawProgress / drawHapticFequency));
            }
            if (_drawProgress == 1 && _lastDrawHaptic != 1)
            {
                _rightController.TriggerHapticPulse((ushort)(2 * drawHapticIntensity));
                _lastDrawHaptic = 1;
            }
            var trajectory = TrajectoryMapper.GetTrajectory(leftPos, 1, _drawProgress * _notchedArrow.flightSpeed * dir);
            _lr.positionCount = trajectory.Length;
            _lr.enabled       = true;
            _lr.SetPositions(trajectory);
        }

        if (rightTrigger < .2f)
        {
            if (_isDrawing)
            {
                //Fire
                effect.Fire(_drawProgress);
                _notchedArrow.Fire(_drawProgress);
                _rightController.TriggerHapticPulse((ushort)(_drawProgress * drawHapticIntensity));
                _leftController.TriggerHapticPulse((ushort)(_drawProgress * drawHapticIntensity));
                _notchedArrow   = null;
                _lr.enabled     = false;
                _drawProgress   = 0;
                _lastDrawHaptic = 0;
                _isDrawing      = false;
                _thisTransform.localRotation = Quaternion.Euler(90, 0, 0);
            }
        }
    }