Пример #1
0
        /*
         * private void AddControllerRotation()
         * {
         *  //Hands pose angle tracking
         *  //Left hand
         *  bool leftHandTrigger;
         *  _walkControllerDevice.TryGetFeatureValue(CommonUsages.gripButton, out leftHandTrigger);
         *  if (leftHandTrigger)
         *  {
         *      _previousLeftPosition = _lHandTransform.localPosition;
         *  }
         *  Vector3 currentLeftVector = _lHandTransform.localPosition - _headTransform.localPosition;
         *  Vector3 previousLeftVector = _previousLeftPosition - _headTransform.localPosition;
         *  previousLeftVector.y = 0.0f;
         *  currentLeftVector.y = 0.0f;
         *  float leftHandAngle = Vector3.SignedAngle(previousLeftVector, currentLeftVector, Vector3.up);
         *  _previousLeftPosition = _lHandTransform.localPosition;
         *  //Right hand
         *  bool rightHandTrigger;
         *  _turnControllerDevice.TryGetFeatureValue(CommonUsages.gripButton, out rightHandTrigger);
         *  if (rightHandTrigger)
         *      _previousRightPosition = _rHandTransform.localPosition;
         *  Vector3 currentRightVector = _rHandTransform.localPosition - _headTransform.localPosition;
         *  Vector3 previousRightVector = _previousRightPosition - _headTransform.localPosition;
         *  previousRightVector.y = 0.0f;
         *  currentRightVector.y = 0.0f;
         *  float rightHandAngle = Vector3.SignedAngle(previousRightVector, currentRightVector, Vector3.up);
         *  _previousRightPosition = _rHandTransform.localPosition;
         *
         *  float rGripInput;
         *  _turnControllerDevice.TryGetFeatureValue(CommonUsages.grip, out rGripInput);
         *  float lGripInput;
         *  _walkControllerDevice.TryGetFeatureValue(CommonUsages.grip, out lGripInput);
         *
         *  //Apply body rotation based on input
         *  if (rGripInput > 0.25f)// && rightHandAngle > 0.005f)
         *  {
         *      //_rightText.text = rightHandAngle.ToString("00.000 " + _previousLeftPosition + " / " + _lHandTransform.localPosition + " - " + _headTransform.localPosition + " - " );
         *      _previousHeadPosition = _headTransform.position;
         *      transform.Rotate(new Vector3(0.0f, -rightHandAngle * 1.0f, 0.0f));
         *      // _userBody.MoveRotation(_userBody.transform.rotation *  Quaternion.Euler(new Vector3(0.0f, -rightHandAngle * 0.5f, 0.0f)));
         *      // transform.Rotate(new Vector3(0.0f, -rightHandAngle * 0.1f, 0.0f));// += new Vector3(0.0f, leftHandAngle, 0.0f);
         *      _userBody.MovePosition(_userBody.position - (_headTransform.position - _previousHeadPosition));
         *  }
         *  //Apply body rotation based on input
         *  if (lGripInput > 0.25f)// && leftHandAngle > 0.005f)
         *  {
         *      // _leftText.text = leftHandAngle.ToString("00.000" + _previousRightPosition + " / " + _rHandTransform.localPosition + " - " + _headTransform.localPosition + " - ");
         *      _previousHeadPosition = _headTransform.position;
         *      //_userBody.MoveRotation( Quaternion.Euler(new Vector3(0.0f, rightHandAngle, 0.0f)));
         *      transform.Rotate(new Vector3(0.0f, -leftHandAngle * 1.0f, 0.0f));// + transform.rotation.eulerAngles);
         *      _userBody.MovePosition(_userBody.position - (_headTransform.position - _previousHeadPosition));
         *  }
         * }*/
        private void AddControllerRotationJoystick()
        {
            //Hands pose angle tracking
            Vector2 stickInput = JAVR_ControllerInput.GetJoystickState(XRNode.RightHand);

            //Apply body rotation based on input

            /*  if (stickInput > Mathf.Abs(0.65f))// && rightHandAngle > 0.005f)
             * {
             *    _previousHeadPosition = _headTransform.position;
             *    transform.Rotate(new Vector3(0.0f, stickInput * 0.5f, 0.0f));
             *    _userBody.MovePosition(_userBody.position - (_headTransform.position - _previousHeadPosition));
             * }*/
            switch (_rotationJoystickState)
            {
            case 0:

                if (Mathf.Abs(stickInput.x) > 0.65f)
                {
                    _rotationJoystickState = 1;
                    _rotationJoystickTimer = Time.time;
                    _isRotationRight       = stickInput.x > 0 ? 1 : -1;
                }
                break;

            case 1:

                if (Time.time - _rotationJoystickTimer <= ANGLE_TURN_TIME)
                {
                    float   f = (ANGLE_TURN / ANGLE_TURN_TIME) * Time.deltaTime * _isRotationRight;
                    Vector3 previousheadPoint = new Vector3(_headTransform.position.x, transform.position.y, _headTransform.position.z);
                    transform.RotateAround(previousheadPoint, Vector3.up, f);
                    //Vector3 currentheadPoint = new Vector3(_headTransform.position.x, transform.position.y, _headTransform.position.z);

                    // transform.position += previousheadPoint - currentheadPoint;
                }
                else
                {
                    _rotationJoystickState = 2;
                    _rotationJoystickTimer = Time.time;
                }
                break;

            case 2:
                if (Time.time - _rotationJoystickTimer >= ANGLE_WAIT_TIME)
                {
                    _rotationJoystickState = 0;
                }
                break;
            }
        }
Пример #2
0
        /*
         * private void AddRocketForce()
         * {
         *  //Get triggers input
         *  float rTriggerInput;
         *  _turnControllerDevice.TryGetFeatureValue(CommonUsages.trigger, out rTriggerInput);
         *  float lTriggerInput;
         *  _walkControllerDevice.TryGetFeatureValue(CommonUsages.trigger, out lTriggerInput);
         *
         *  //Apply rocket thrurst based on input
         *  if (rTriggerInput > 0.05f || lTriggerInput > 0.05f)
         *  {
         *      _userBody.AddForce((rTriggerInput * _rHandTransform.up * 4.0f) + (lTriggerInput * _lHandTransform.up * 4.0f));
         *  }
         *  if (_userBody.velocity.magnitude > MAX_SPEED)
         *  {
         *      _userBody.velocity = _userBody.velocity.normalized * MAX_SPEED;
         *  }
         * }*/
        private void AddWalkingForce()
        {
            Vector2 walkStrafe = JAVR_ControllerInput.GetJoystickState(XRNode.LeftHand);

            walkStrafe.y = walkStrafe.y <0.25f && walkStrafe.y> -0.15f ? 0.0f : walkStrafe.y;
            walkStrafe.x = walkStrafe.x <0.25f && walkStrafe.x> -0.15f ? 0.0f : walkStrafe.x;

#if UNITY_EDITOR
            if (Input.GetKey(KeyCode.W))
            {
                walkStrafe.y += 1.0f;
            }
            if (Input.GetKey(KeyCode.S))
            {
                walkStrafe.y -= 1.0f;
            }
            if (Input.GetKey(KeyCode.D))
            {
                walkStrafe.x += 1.0f;
            }
            if (Input.GetKey(KeyCode.S))
            {
                walkStrafe.x -= 1.0f;
            }
#endif
            Vector3 hitNormal;
            Vector3 hitPoint;
            bool    contact      = _userController.isGrounded;//DetectGround(out hitNormal, out hitPoint);
            bool    slopeContact = DetectSlope(new Vector3(walkStrafe.x, 0.0f, walkStrafe.y), out hitNormal);
            hitNormal = Vector3.Dot(hitNormal, Vector3.up) >= 0.95f ? Vector3.up : hitNormal;
            Vector3 forwardVector = Vector3.ProjectOnPlane(new Vector3(_headTransform.forward.x, 0.0f, _headTransform.forward.z), hitNormal).normalized;
            Vector3 rightVector   = Vector3.ProjectOnPlane(new Vector3(_headTransform.right.x, 0.0f, _headTransform.right.z), hitNormal).normalized;
            Vector3 walkingForce  = forwardVector * walkStrafe.y * 2.0f +
                                    rightVector * walkStrafe.x * 2.0f;


            if (contact || slopeContact)
            {
                //        if (slopeContact)
                //           walkingForce += Vector3.up * walkingForce.y;

                _userControllerVelocity = new  Vector3(_userControllerVelocity.x, 0.0f, _userControllerVelocity.z);
                _userControllerVelocity = walkingForce;
            }
            else
            {
                _userControllerVelocity += (Physics.gravity * Time.deltaTime);
                _userControllerVelocity -= _userControllerVelocity * ((0.004f) * Mathf.Pow(_userControllerVelocity.magnitude, 2.0f) * Time.deltaTime);
            }
            _userController.Move(_userControllerVelocity * Time.deltaTime);
        }
        // Update is called once per frame
        void Update()
        {
            _validContact   = false;
            _handController = InputDevices.GetDeviceAtXRNode(s_handNode);
            Vector2 stickInput = JAVR_ControllerInput.GetJoystickState(s_handNode);

            switch (_state)
            {
            //Controller joystick is idle, nothing happens.
            case 0:

                s_teleportGizmo.SetActive(false);


                if (stickInput.y > 0.5f)
                {
                    _state = 1;

                    if (!otherTeleport.TeleportSwap())
                    {
                        _state = 2;
                    }
                }

                break;

            //If the raycast hits a teleportable surface, the teleport point gizmo will apear at the contact point.
            case 1:

                RaycastHit hit;
                Ray        ray = CalculateParabolicRay();// new Ray();

                //ray.direction = transform.forward;
                //ray.origin = transform.position;
                EnableDebugGizmos(true);
                if (Physics.Raycast(ray, out hit, 10.0f, s_teleportableSurfaces))
                {
                    if (Vector3.Dot(hit.normal, Vector3.up) >= 0.9f)
                    {
                        _validContact = true;
                        s_teleportGizmo.SetActive(true);
                        s_parabolicLineGizmo.enabled = true;
                        s_teleportPoint.position     = hit.point + (Vector3.up * 0.05f);
                        //teleportPoint.forward = hit.normal * -1.0f;
                        s_teleportPoint.eulerAngles += Vector3.forward * (Time.fixedDeltaTime) * 360.0f * 0.25f;
                        DrawParable();

                        /* SnapZone sz = hit.collider.GetComponent<SnapZone>();
                         * if (sz != null)
                         * {
                         *   if (!sz.activated)
                         *   {
                         *       sz.Activate(true);
                         *   }
                         *   previousSnapZone = sz;
                         * }
                         * else
                         * {
                         *   if (previousSnapZone != null)
                         *   {
                         *       previousSnapZone.Activate(false);
                         *       previousSnapZone = null;
                         *   }
                         * }*/

                        if (stickInput.y <= 0.25f)
                        {
                            _state            = 3;
                            _dashingStart     = Time.time;
                            _originalPosition = s_cameraRig.position;
                            _positionOffset   = (new Vector3(s_headTransform.position.x, 0.0f, s_headTransform.position.z) - new Vector3(s_cameraRig.position.x, 0.0f, s_cameraRig.position.z));
                            _dashingTime      = (_originalPosition - (_teleportPosition + _positionOffset)).magnitude / _dashingSpeed;
                            if (_dashingTime > 0.1f)
                            {
                                _dashingTime = 0.1f;
                            }
                            _dashingTime = 0.1f;
                            EnableDebugGizmos(false);

                            /*if (sz != null)
                             * {
                             *  teleportPosition = sz.transform.position;
                             * }
                             * else
                             * {
                             *  teleportPosition = hit.point;
                             * }*/
                            _teleportPosition = hit.point;
                            break;
                        }
                    }
                    else
                    {
                        s_teleportGizmo.SetActive(false);
                        DrawParable();
                        // parabolicLineGizmo.enabled = false;
                        //
                    }
                }
                else
                {
                    s_teleportGizmo.SetActive(false);
                    DrawParable();
                    //   parabolicLineGizmo.enabled = false;
                }
                if (stickInput.y <= 0.25f)
                {
                    EnableDebugGizmos(false);
                    _state = 0;
                }
                break;

            //if the other teleporter gets activated we waut till joystick is reset.
            case 2:

                s_teleportGizmo.SetActive(false);
                EnableDebugGizmos(false);
                if (stickInput.y <= 0.25f)
                {
                    _state = 0;
                }

                break;

            //Dashing the camera rig
            case 3:

                s_teleportGizmo.SetActive(false);
                EnableDebugGizmos(false);
                if (Time.time - _dashingStart <= _dashingTime)
                {
                    s_cameraRig.position = Vector3.Lerp(_originalPosition, _teleportPosition - _positionOffset, (Time.time - _dashingStart) / _dashingTime);
                }
                else
                {
                    s_cameraRig.position = _teleportPosition - _positionOffset;
                    _state = 0;
                }
                break;
            }
        }