Search Unity

SteamVR Moving in the Direction the Player is Looking

Discussion in 'AR/VR (XR) Discussion' started by yetikeiko, Jan 7, 2019.

  1. yetikeiko

    yetikeiko

    Joined:
    Sep 26, 2018
    Posts:
    1
    Hello, we are trying to develop a VR game using the HTC Vive. We want the player to move in the direction in which they are looking when the touch-pad is pressed, but instead we move in the same direction no matter which way the player is looking. Here is a snippet of our Update function for movement:

    Code (CSharp):
    1. Vector2 leftTouchPadValue = leftTouchPadAction.GetAxis(SteamVR_Input_Sources.LeftHand);
    2. bool leftWasFalse = SteamVR_Input._default.inActions.TouchPadLeftPress.GetStateDown(SteamVR_Input_Sources.LeftHand);
    3. bool leftWasTrue = SteamVR_Input._default.inActions.TouchPadLeftPress.GetLastState(SteamVR_Input_Sources.LeftHand);
    4. bool rightWasFalse = SteamVR_Input._default.inActions.TouchPadRightPress.GetStateDown(SteamVR_Input_Sources.RightHand);
    5.  
    6. if (leftWasFalse)
    7. {
    8.         transform.localPosition -= new Vector3(Time.deltaTime * leftTouchPadValue.x * speed, 0f, Time.deltaTime * leftTouchPadValue.y * speed);
    9. } //end if wasFalse
    10.  
    11.      
    12. else if (leftWasTrue)
    13. {
    14.         transform.localPosition -= new Vector3(Time.deltaTime * leftTouchPadValue.x * speed, 0f, Time.deltaTime * leftTouchPadValue.y * speed);
    15. } //end if wasTrue
    Any help is appreciated!
     
    Last edited: Jan 9, 2019
  2. GameDevCouple_I

    GameDevCouple_I

    Joined:
    Oct 5, 2013
    Posts:
    1,306
    Always worth to do a quick google first and read the official tutorials, as this question has been asked and answered countless times in a variety of ways:

    https://answers.unity.com/questions/616195/how-to-make-an-object-go-the-direction-it-is-facin.html

    https://answers.unity.com/questions/957976/trying-to-make-a-game-object-move-in-the-direction.html

    https://answers.unity.com/questions/292178/make-object-move-in-the-direction-is-facing-when-r.html

    https://gamedev.stackexchange.com/q...nding-on-the-direction-he-is-facing-c-unity3d

    TLDR: How does your code know what "forward" is ? Basically you need to include transform.forward in your calculations https://docs.unity3d.com/ScriptReference/Transform-forward.html (the example on that even explains how to do this)