-
-
Save kormyen/a1e3c144a30fc26393f14f09989f03e1 to your computer and use it in GitHub Desktop.
| using UnityEngine; | |
| using System.Collections; | |
| public class GyroCamera : MonoBehaviour | |
| { | |
| // STATE | |
| private float _initialYAngle = 0f; | |
| private float _appliedGyroYAngle = 0f; | |
| private float _calibrationYAngle = 0f; | |
| private Transform _rawGyroRotation; | |
| private float _tempSmoothing; | |
| // SETTINGS | |
| [SerializeField] private float _smoothing = 0.1f; | |
| private IEnumerator Start() | |
| { | |
| Input.gyro.enabled = true; | |
| Application.targetFrameRate = 60; | |
| _initialYAngle = transform.eulerAngles.y; | |
| _rawGyroRotation = new GameObject("GyroRaw").transform; | |
| _rawGyroRotation.position = transform.position; | |
| _rawGyroRotation.rotation = transform.rotation; | |
| // Wait until gyro is active, then calibrate to reset starting rotation. | |
| yield return new WaitForSeconds(1); | |
| StartCoroutine(CalibrateYAngle()); | |
| } | |
| private void Update() | |
| { | |
| ApplyGyroRotation(); | |
| ApplyCalibration(); | |
| transform.rotation = Quaternion.Slerp(transform.rotation, _rawGyroRotation.rotation, _smoothing); | |
| } | |
| private IEnumerator CalibrateYAngle() | |
| { | |
| _tempSmoothing = _smoothing; | |
| _smoothing = 1; | |
| _calibrationYAngle = _appliedGyroYAngle - _initialYAngle; // Offsets the y angle in case it wasn't 0 at edit time. | |
| yield return null; | |
| _smoothing = _tempSmoothing; | |
| } | |
| private void ApplyGyroRotation() | |
| { | |
| _rawGyroRotation.rotation = Input.gyro.attitude; | |
| _rawGyroRotation.Rotate(0f, 0f, 180f, Space.Self); // Swap "handedness" of quaternion from gyro. | |
| _rawGyroRotation.Rotate(90f, 180f, 0f, Space.World); // Rotate to make sense as a camera pointing out the back of your device. | |
| _appliedGyroYAngle = _rawGyroRotation.eulerAngles.y; // Save the angle around y axis for use in calibration. | |
| } | |
| private void ApplyCalibration() | |
| { | |
| _rawGyroRotation.Rotate(0f, -_calibrationYAngle, 0f, Space.World); // Rotates y angle back however much it deviated when calibrationYAngle was saved. | |
| } | |
| public void SetEnabled(bool value) | |
| { | |
| enabled = true; | |
| StartCoroutine(CalibrateYAngle()); | |
| } | |
| } |
Goooooooood
@kormyen I like your script, but is there away to avoid that abrupt snap on the start? Which I assume has to do with the calibration. Maybe do it like threejs does? Take a look at this, no snapping. If you find a way to remove the initial snap your script will be perfect!
Thank you!!! Spent like 40$ on plugins before I found this, this should be included in unity gyro sample!
you are the best man!
This code is amazing!
Thank you!
Thanks a lot for the solution
@that1phillips
How exactly did you manage to stop the jittering?
@that1phillips
Hi. You mentioned controlling the slerp? I'm not very experienced with coding sorry. Can you please give me an example of what you mean?
Is there a way to lock every rotation except Y axis?
Nice project broo
How to I lock the Y axis rotation
Greetings, sorry for kind of necro-ing this, but I have one question. How do I "swap" the rotation logic for looking up and down?
Everything else works perfectly, but the camera moves up, when I rotate the camera down and vice versa. How do I change this`?
Thank you for sharing this. I extended it to include X and Z axis when calibrating so you can re-center in any direction and at a variable speed.
https://gist.github.com/blevok/0b5f4c821f019dd1d74fce98c9de3d5f