Search Unity

Help with simple movement prototypes with VIVE

Discussion in 'VR' started by sakaripollari, Jan 14, 2019.

  1. sakaripollari

    sakaripollari

    Joined:
    Jan 14, 2019
    Posts:
    4
    Hey, everyone! Been learning to use the Vive in Unity with SteamVR 2.0. Found some good tutorials to get started with, but now I'm dabbling with movement methods.

    I was able to move myself around doing this:

    Code (CSharp):
    1. public class cl_movementVR : MonoBehaviour {
    2.     public Transform player;
    3.     public float speed = 1.0f;
    4.     private Vector3 handmove;
    5.     public SteamVR_Action_Boolean m_GrabAction = null;
    6.     private SteamVR_Behaviour_Pose m_Pose = null;
    7.  
    8.     void Awake() {
    9.         m_Pose = GetComponent<SteamVR_Behaviour_Pose>();
    10.     }
    11.     void Update() {
    12.         handmove = m_Pose.GetVelocity() * -1;
    13.  
    14.         if (m_GrabAction.GetState(m_Pose.inputSource)) {
    15.             player.position += handmove * speed;
    16.         }
    17.     }
    18. }
    19.  
    This works fine, moving the player in the direction you move the controller while squeezing. But I would like to be able to also change the scale and rotation by holding both controllers.

    I'm thinking something like getting the positions of both controllers and looking at the vector between the two and using it's rotation in the Y axis to turn the CameraRig. That should work. And scale by just the distance between the controllers.

    If anyone has simple methods of moving in VR using the controllers, I'd love to take a look!

    Thanks!
     
  2. sakaripollari

    sakaripollari

    Joined:
    Jan 14, 2019
    Posts:
    4
    My code currently.

    Code (CSharp):
    1. public class cl_movementVR : MonoBehaviour {
    2.     [HideInInspector]
    3.     public Transform player;
    4.     public float speed = 1.0f;
    5.     public float rotspeed = 1.0f;
    6.     private Vector3 m_lefthand, m_righthand;
    7.     private Vector3 handrot;
    8.     public Transform t_lefthand, t_righthand;
    9.     public SteamVR_Action_Boolean m_GrabAction = null;
    10.     public SteamVR_Behaviour_Pose m_PoseL, m_PoseR;
    11.     private float distance;
    12.     private Vector3 initialDir, currentDir;
    13.     private bool startTurn = false;
    14.     private float angleToTurn;
    15.     private float oldAngle;
    16.     private Vector3 m_oldleft = Vector3.zero, m_oldright = Vector3.zero;
    17.  
    18.     void Awake() {
    19.         player = GetComponent<Transform>();
    20.     }
    21.  
    22.     void Update() {
    23.         // Left hand velocity
    24.         m_lefthand = m_PoseL.GetVelocity();
    25.         // Right hand velocity
    26.         m_righthand = m_PoseR.GetVelocity();
    27.  
    28.         // Left grab
    29.         if (m_GrabAction.GetState(m_PoseL.inputSource)) {
    30.             m_lefthand *= -1;
    31.             // Move player by the velocity of the nonpressing hand PLUS the velocity inverse to the grabbing hand
    32.             player.position += (m_righthand + m_lefthand) * speed;
    33.         }
    34.         // Right grab
    35.         if (m_GrabAction.GetState(m_PoseR.inputSource)) {
    36.             m_righthand *= -1;
    37.             // Move player by the velocity of the nonpressing hand PLUS the velocity inverse to the grabbing hand
    38.             player.position += (m_righthand + m_lefthand) * speed;
    39.         }
    40.  
    41.         // Take initial direction for rotation action
    42.         if (m_GrabAction.GetState(m_PoseL.inputSource) && m_GrabAction.GetState(m_PoseR.inputSource) && startTurn == false) {
    43.             startTurn = true;
    44.             Vector3 a = t_lefthand.position - t_righthand.position;
    45.             a[1] = 0; // Make Y zero, so we'll only rotate around the Y axis.
    46.             initialDir = a;
    47.             oldAngle = player.localEulerAngles.y;
    48.         }
    49.         // While both hands are gripping, we'll rotate
    50.         if (startTurn) {
    51.             Vector3 b = t_lefthand.position - t_righthand.position;
    52.             b[1] = 0; // Make Y zero, so we'll only rotate around the Y axis.
    53.             currentDir = b;
    54.             angleToTurn = Vector3.Angle(initialDir, currentDir);
    55.             player.localEulerAngles = new Vector3(0, oldAngle + angleToTurn, 0);
    56.         }
    57.         // Stop rotating if either grip lets go
    58.         if (m_GrabAction.GetStateUp(m_PoseL.inputSource) || m_GrabAction.GetStateUp(m_PoseR.inputSource)) {
    59.             startTurn = false;
    60.         }
    61.     }
    62. }
    63.  


    The moving works fine. The rotation is broken. Not only does it not rotate properly, if I do rotate, then the movement is broken.

    I think it's because I rotate the playspace (as in the [CameraRig] object from SteamVR), and once that is turned, the m_lefthand = m_PoseL.GetVelocity(); returns a value in relation to world coordinated, which is misaligned in relation to the playspace.

    Is there a different method of rotating the player without rotating the playspace?

    I've been trying to find a script for snap turning to see how that is done, but weirdly enough I can't find one.