Last active
May 20, 2021 21:09
-
-
Save wtekteam/85059c86f91c42752bb4 to your computer and use it in GitHub Desktop.
Gyroscope Head Tracking for Unity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
// Activate head tracking using the gyroscope | |
// The script must be linked to the main camera | |
public class HeadTracking : MonoBehaviour | |
{ | |
// The initials orientation | |
private int initialOrientationX; | |
private int initialOrientationY; | |
private int initialOrientationZ; | |
// Initialization | |
void Start () | |
{ | |
// Activate the gyroscope | |
Input.gyro.enabled = true; | |
Input.gyro.updateInterval = 0.01f; | |
// Save the firsts values | |
initialOrientationX = (int)Input.gyro.rotationRateUnbiased.x; | |
initialOrientationY = (int)Input.gyro.rotationRateUnbiased.y; | |
initialOrientationZ = (int)-Input.gyro.rotationRateUnbiased.z; | |
} | |
// Update is called once per frame | |
void Update () | |
{ | |
transform.Rotate (initialOrientationX -Input.gyro.rotationRateUnbiased.x, initialOrientationY -Input.gyro.rotationRateUnbiased.y, initialOrientationZ + Input.gyro.rotationRateUnbiased.z); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment