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.
Answer by Mmmpies · May 23, 2016 at 01:43 PM
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! :)
Oh and if you get a steep slope it'll fall apart trying to set the height so lock off anything too steep.
Answer by ProtoPottyGames · May 17, 2016 at 11:17 PM
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.
How would I need to change the parent object's orientation?
The parent's orientation can be changed in the same way that you would normally change it with a "regular" controller. You just need to change the controller input information in your script so it uses the vive controller's inputs ins$$anonymous$$d. Try sticking the camerarig on one of the vehicle prefabs in the standard assets folder (as a child) and test it out with an Xbox 360 controller (or whatever you usually use.) Then you can just swap out your original inputs with the S$$anonymous$$mVR ones.
Here's a script I found on git. There's probably a better one out there to reference somewhere, but this one looks to have most, if not all of the vive controller's button information. (Haven't had much time to do a really good look over it though, sorry.) I'll have to double check it later tonight to confirm.
Ha! $$anonymous$$y bad, scratch that.. Here's your pony. ;)
Ha! Double scratch that for anyone still needing anything whatsoever regarding vive input information. Do this ins$$anonymous$$d..
Step 1.) Watch this guy's brilliant (ADD friendly) videos:
Theston $$anonymous$$ Fox - YouTube https://m.youtube.com/watch?v=hr5OoSCksnY
Step 2.) Use that guy's amazing scripts.
Step 3.) Live the dream. ;D
Your answer
Follow this Question
Related Questions
Movement input problem? 1 Answer
Character Movement on Cylinder Surface 0 Answers
VR Vive Tracker barely moving 1 Answer
Isometric movement for MoveTowards 0 Answers