- Home /
 
How can I let the player rotate with several speeds in VR, HTC Vive?
Hello Unity Comunity,
I am trying to create an VR Game in which one is flying around through space in FPS-view. To control the flight, the player should stretch his arms to left and right like wings of a plane. With leaning right or left, the player should then rotate on his z axis, right like a plane.
I was able to let him rotate, but always in the same speed. My Intention is that the speed of the rotation depends on how strong you lean to a certain direction. I tryied to use the distance between the left controller and the head on the y-axis, but it is only activating the rotation if the controller is lower than the head, it is not affecting the speed of the rotation.
PS: In the script you see also the lines for flying forward by stretching both arms in the fly-direction. I know that contradicts with the leaning, I will replace it later with accelerating by pulling the trigger on the controller.
I hope you can help me, thank you very much
Greetings Slaytanic23
  using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
   
  public class fly2 : MonoBehaviour
  {
      public GameObject player;
      public float speed;
      public Transform head;
   
      public SteamVR_TrackedObject leftHand;
      public SteamVR_TrackedObject rightHand;
   
      private bool isflying = false;
   
   
   
      private void Start()
      {
   
     player = GameObject.FindGameObjectWithTag("MainCamera");
   
      }
   
   
   
      void FixedUpdate()
      {
   
       
  // flying forward
          var lDevice = SteamVR_Controller.Input((int)leftHand.index);
          var rDevice = SteamVR_Controller.Input((int)rightHand.index);
   
              if (lDevice.GetTouchDown(SteamVR_Controller.ButtonMask.Trigger) || rDevice.GetTouchDown(SteamVR_Controller.ButtonMask.Trigger))
              {
                  isflying = !isflying;
   
              }
   
              if (isflying)                
              {
                  Vector3 leftDir = leftHand.transform.position - head.position;
                  Vector3 rightDir = rightHand.transform.position - head.position;
                  Vector3 dir = leftDir + rightDir;
   
                  transform.position += dir * speed;
              }
   
   
    //rotating
          float rotateright = (player.transform.localPosition.y - leftHand.transform.position.y);
   
          float rotateleft = (leftHand.transform.position.y - player.transform.localPosition.y);
   
   
              if (leftHand.transform.localPosition.y -0.05 < player.transform.localPosition.y)
              {
                  transform.Rotate(0,0, rotateleft*Time.deltaTime);
   
              }
   
              if (leftHand.transform.localPosition.y +0.05> player.transform.localPosition.y)
              {
                  transform.Rotate(0,0, rotateright*Time.deltaTime);
              }
      }
  }
 
              Your answer
 
             Follow this Question
Related Questions
Manipulating the location of the VR controllers (Vive/SteamVR) in script 0 Answers
How to setup the Hololens spectator project and How to record? 0 Answers
Unity Abstract Parent Class not in Build, but in Editor 0 Answers
How to access Raycast's gameObject from the Gear VR control? 0 Answers
Gaze Pointer (Gvr reticle) seems double 2 Answers