Last active
September 22, 2023 13:37
-
-
Save kormyen/a1e3c144a30fc26393f14f09989f03e1 to your computer and use it in GitHub Desktop.
Unity3D script for rotating camera with a phone's gyro. Includes smoothing and initial offset. Edited from https://forum.unity3d.com/threads/sharing-gyroscope-camera-script-ios-tested.241825/
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; | |
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()); | |
} | |
} |
@that1phillips
How exactly did you manage to stop the jittering?
It's in controlling the slurp. What I've learned is that it also depends on
the hardware in the phone. Some phones do better than others. I still
haven't finished the project, but have had over 250 people testing it on
the android store.
…On Wed, Apr 7, 2021, 12:22 AM BranCo2 ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
@that1phillips <https://github.com/that1phillips>
How exactly did you manage to stop the jittering?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<https://gist.github.com/a1e3c144a30fc26393f14f09989f03e1#gistcomment-3696097>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AK6M5MVZ56BEN3WS4NF7WC3THPMXHANCNFSM4HPSRXJQ>
.
@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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks a lot for the solution