Traditional movement in vr using the vive controllers?

So I am trying to make a game in which the player moves using the trackpad on one of the HTC Vive controllers. Right now I have a script that can move the camerarig with the trackpad, but it will only move in the direction the camerarig is facing when it starts (so if the player looks left and presses forward on the trackpad, in their pov they will be moving to the right.) How can I get this working so that the camerarig moves in the direction the player is facing?

using UnityEngine;
using System.Collections;
using Valve.VR;

public class touchPad : MonoBehaviour
{
    public GameObject player;
    //player is the camerarig
    SteamVR_Controller.Device device;
    SteamVR_TrackedObject controller;

    Vector2 touchpad;

    void Start()
    {
        controller = gameObject.GetComponent<SteamVR_TrackedObject>();
    }

    // Update is called once per frame
    void Update()
    {
        device = SteamVR_Controller.Input((int)controller.index);
        //If finger is on touchpad
        if (device.GetTouch(SteamVR_Controller.ButtonMask.Touchpad))
        {
            //Read the touchpad values
            touchpad = device.GetAxis(EVRButtonId.k_EButton_SteamVR_Touchpad);

            player.transform.position = new Vector3(player.transform.position.x + (-touchpad.y/100f), player.transform.position.y, player.transform.position.z + (touchpad.x/100f));
        }
    }
}

Thanks.

It’s tricky @nmgh101 because the head look isn’t the rotation of the Camera, forward is still forward for the Vive setup no matter where your head’s looking and if you rotate the parent position to be the direction your head’s facing then you’ll just end in a flat spin.

I’ve setup a controller (right or left - whichever your preference is) with this script…

using UnityEngine;
using System.Collections;
using Valve.VR;

public class myTouchpad : MonoBehaviour
{
	public GameObject player;

	SteamVR_Controller.Device device;
	SteamVR_TrackedObject controller;

	Vector2 touchpad;

	private float sensitivityX = 1.5F;
	private Vector3 playerPos;

	void Start()
	{
		controller = gameObject.GetComponent<SteamVR_TrackedObject>();
	}

	// Update is called once per frame
	void Update()
	{
		device = SteamVR_Controller.Input((int)controller.index);
		//If finger is on touchpad
		if (device.GetTouch(SteamVR_Controller.ButtonMask.Touchpad))
		{
			//Read the touchpad values
			touchpad = device.GetAxis(EVRButtonId.k_EButton_SteamVR_Touchpad);


			// Handle movement via touchpad
			if (touchpad.y > 0.2f || touchpad.y < -0.2f) {
				// Move Forward
				player.transform.position -= player.transform.forward * Time.deltaTime * (touchpad.y * 5f);

				// Adjust height to terrain height at player positin
				playerPos = player.transform.position;
				playerPos.y = Terrain.activeTerrain.SampleHeight (player.transform.position);
				player.transform.position = playerPos;
			}

			// handle rotation via touchpad
			if (touchpad.x > 0.3f || touchpad.x < -0.3f) {
				player.transform.Rotate (0, touchpad.x * sensitivityX, 0);
			}

			//Debug.Log ("Touchpad X = " + touchpad.x + " : Touchpad Y = " + touchpad.y);
		}
	}
}

Have a parent to the camera, put a rigidbody with Y constrained on both Rotation and Position. Put that script on your controller and drag the camera Parent object onto the public player object.

It’s a bit of a combination of a traditional controller WASD/mouse, forward backward and turn whilst your head can still look around.

Really I’d add an option to teleport as well as some people will feel ill with this approach. I guess I’m not one of them given how much time I spent spinning around on the floor trying to get the script to work! :slight_smile:

Oh and if you get a steep slope it’ll fall apart trying to set the height so lock off anything too steep.

You’ll have to parent it to another object and move the parent object instead. The steamvr camerarig will only face the direction of it’s original orientation.