- Home /
Convert GetAxis (mouse) to VR head movement
Hi! I'm making my first VR prototype. It's just a first person dynamic crosshair test. As the newbie I am, I made it all work... on PC, using a mouse to control the view, without any VR integration at the beginning. The question would be: is there any "easy" way to just turn the values I get with Input.GetAxis ("Mouse X") or ("Mouse Y") into the head tracking values used for VR? I've found that there are some scripts which allow you to easily implement VR camera, input, interactive objects, etc. if you use them from the start. But now that everything is almost ready, I wonder if there's an equivalent function to Input.GetAxis for VR tracking.
This is the (ultra basic) script I use to move the camera with the mouse:
using UnityEngine; using System.Collections;
public class CamMovement : MonoBehaviour { public float speedH = 2.0f; public float speedV = 2.0f;
private float yaw = 0.0f;
private float pitch = 0.0f;
void Update ()
{
yaw += speedH * Input.GetAxis("Mouse X");
pitch -= speedV * Input.GetAxis("Mouse Y");
transform.eulerAngles = new Vector3(pitch, yaw, 0.0f);
}
}
And this is another thing I'm doing on another script: filling a bar following the vertical mouse movement under specific circumstances:
void Fill ()
{
if (
(Input.GetAxis ("Mouse X") < horMargin) &&
(Input.GetAxis ("Mouse X") > -horMargin))
{
filler += fillRate * Input.GetAxis ("Mouse Y");
filler = Mathf.Clamp (filler, -10, 0);
}
}
}
Any possible way to make this all VR tracking-ready without loads of adapting work? Thank you very much.
Answer by AMorgado · Mar 17, 2017 at 10:03 PM
Just realized I could tell how I eventually solved this. The first basic point was making the camera work when using the VR headset. Recent Unity versions seem to make it easy: just put your camera within a parent game object and attach your script to this parent object. It worked perfectly.
But my real problem was that I had another input that depended on the GetAxis input (a bar that was supposed to fill following the player's gaze), and that wasn't working at all. Since that probably wasn't a proper input for VR, I tried using the game object's rotation to "simulate" that gaze. But even when the "VR camera" takes control of the movement you set for your original camera (now scripted on the parent game object), it didn't seem to be really taking into account any transform.
Well, after all this frustrating process (I know there are easier ways to have done it from the start...), the solution was not that difficult: using InputTracking.GetLocalRotation(VRNode.Head).eulerAngles.x (as I wanted to track vertical head movement).
Yes, I know it's not that hard of an issue, but for me it was difficult to solve properly and, since nobody fed this thread, I just wanted to decorate it with a closure. Thanks!
Hi @A$$anonymous$$orgado.... I've printed this code on screen at VR mode but it gave me nothing
InputTracking.GetLocalRotation(VRNode.Head).eulerAngles.x;
Actually i want to create a VR Character and i want this Character to rotate when i turn my head.... I want to create a VR Character and control it with Bluetooth controller!! when i press forward my character always move forward and doesn't move through my head direction....
Please Help me .....
Hi, @Hooman-BZ-1989. I'm sorry for taking this much time to answer, and even more sorry for not feeling I will be of great help.
In my case, I used InputTracking.GetLocalRotation(VRNode.Head).eulerAngles.x because I needed to get the rotation over the X axis, which translates into head's up and down movement.
If you want to apply your right-left head's rotation to the player's, while it's moving around on the ground, maybe you should use eulerAngles.y.
Apart from that idea, I don't think I can help you further. I'm not a programmer and just get by each time I need to do something very specific.
I hope you work it out (or you have already done). Regards.