- Home /
how to get eye position and rotation steamvr
hey I am making a game where there are portals and I need to get both eyes position and rotation to get the portal effect.
how do I get individual eye positions? thanks in advance.
Answer by icehex · Apr 29, 2020 at 07:04 AM
Latest Unity XR (mixed and virtual reality) documentation for getting input: https://docs.unity3d.com/2020.2/Documentation/ScriptReference/XR.InputTracking.html
Since it's fairly new, it isn't very detailed. Also I've never done this before, but give this code snippet a go. It compiles for me but I don't have a VR headset to test it with.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR;
public class xrnodetest : MonoBehaviour
{
public List<XRNodeState> nodeStates;
private XRNodeState single_xrnode;
private Vector3 left_eye_pos, right_eye_pos;
private Quaternion left_eye_rot, right_eye_rot;
// Start is called before the first frame update
void Start()
{
nodeStates = new List<XRNodeState>();
// Add left eye node to the nodeStates list, which will be represented by nodeStates[0]
single_xrnode.nodeType = XRNode.LeftEye;
nodeStates.Add(single_xrnode);
// Add right eye node to the nodeStates list, which will be represented by nodeStates[1]
single_xrnode.nodeType = XRNode.RightEye;
nodeStates.Add(single_xrnode);
}
// Update is called once per frame
void Update()
{
InputTracking.GetNodeStates(nodeStates);
// Try to get the parameters and place them into the correct variables
nodeStates[0].TryGetPosition(out left_eye_pos);
nodeStates[0].TryGetRotation(out left_eye_rot);
nodeStates[1].TryGetPosition(out right_eye_pos);
nodeStates[1].TryGetRotation(out right_eye_rot);
Debug.Log("Left eye position: " + left_eye_pos);
Debug.Log("Left eye rotation: " + left_eye_rot);
Debug.Log("Right eye position: " + right_eye_pos);
Debug.Log("Right eye rotation: " + right_eye_rot);
}
}
I should add that you'll have to name the script "xrnodetest" or change "xrnodetest" in the code above to the name of your script, then attach it to an empty game object or a game object that makes sense so that Start() and Update() get executed when you hit Play.
Answer by TheMaster_123 · Apr 29, 2020 at 07:08 AM
that seems to be for unity 5.6 I am using the latest version of unity.
and I can't access InputTracking it does not appear for me.
Hi @TinyDays please reply to my answer above ins$$anonymous$$d of creating new answers to make a comment. You're right I had the old reference, thanks for the catch! I've updated the answer and put together for you some code you should be able to copy and paste to test. Let me know if it works, I'm also interested in the outcome :-). Thanks!
Your answer
Follow this Question
Related Questions
Mathf.Abs and How to play animation only if x,z transform has changed by 0.2 units ? 2 Answers
How to get the direction my Steam VR controller is pointing? 0 Answers
during build object moves different position than editor(vr) 1 Answer
Line renderer transform position problem 1 Answer
transform.position 3 Answers