- Home /
Unity/SteamVR changing coordinate system based on camera view
Hello,
I am trying to get started with developing VR in unity using an HTC Vive and the SteamVR plugin. I am able to see everything in VR and translate it using controllers, but the coordinate system is very confusing. The object only goes right when I press the right touchpad when it is oriented a certain way. Once it is rotated differently, its axes are no longer in line with its initial ones. Also, if I move in the scene using the headset tracking, the coordinate system obviously doesn't move. This means that I can press right and it will go "forward".
I looked into some coordinate system changes such as WorldToViewportPoint to try and make the coordinate system the same as the camera, but to no avail (I am not sure if this is even possible, or if it didn't work because I executed it incorrectly). I also looked into making the model I am trying to translate a child to the camera attached to the headset, as well as the other way around. Nothing worked as I imagined. I am obviously new to C# and Unity, so I do not know many intricacies of things like this.
using UnityEngine; using System.Collections; using System.Reflection; using Valve.VR;
[RequireComponent(typeof(Camera))] public class MonoBehaviour { public class conrotate : MonoBehaviour {
public GameObject head;
public GameObject testhead;
SteamVR_TrackedObject trackdeObjec;
Camera headCam;
void Awake()
{
//获取手柄上的这个组件
trackdeObjec = GetComponent<SteamVR_TrackedObject>();
// headCam = head.GetComponent<Camera>();
}
//void Start () {
//}
uint i; // iterative counter
void FixedUpdate () {
var device = SteamVR_Controller.Input ((int)trackdeObjec.index);
float translation = Time.deltaTime * 10;
float t =Time.deltaTime/100;
//translation
if (device.GetPress (SteamVR_Controller.ButtonMask.Touchpad)&&(!device.GetPress (SteamVR_Controller.ButtonMask.Trigger)))
{
Vector2 ccc = device.GetAxis ();
float angle1 = VectorAngle (new Vector2 (1, 0), ccc);
if (angle1 > 45 && angle1 < 135) {
// Vector3 viewPos = headCam.WorldToViewportPoint(transform.position);
// Debug.Log (testhead.transform.position);
// Debug.Log (viewPos);
testhead.transform.Translate(Vector3.up*-translation,Space.World);
Debug.Log ("down");
}
This is just an example of what I attempted with the WorldToViewportPoint. Everything commented out is my attempt. I do not know if I am using it correctly or not. I can get the Debug.Log to show "viewPos" (at least sometimes) and it has different coordinates, but the translation remains the same. The coordinates also go above 1 and below 0, which I think is strange for Viewport.
TL;DR I want to change the axes on an object to be those used by the VR camera on the headset so when I press right it always goes right relative to what I am seeing. I have no idea if this is possible, nor how to do it. Send help please, anything is appreciated. Thanks in advance.
Answer by danilogasques · Jun 29, 2017 at 07:06 PM
It seems that you are looking for the forward
direction of your headCam
transform. Take a look at the documentation here: https://docs.unity3d.com/ScriptReference/Transform-forward.html