Files
Aplikasi-VR-Edukasi-Mitigas…/Assets/Scripts/GyroKepala.cs
Savina Rizdafayi d7120c397a Initial commit
2025-07-12 19:53:40 +07:00

49 lines
1.2 KiB
C#

using UnityEngine;
public class GyroKepala : MonoBehaviour
{
private Gyroscope gyro;
private bool gyroEnabled;
// Koreksi rotasi agar sesuai dengan orientasi Unity
private Quaternion rotationFix = Quaternion.Euler(90f, 0f, -90f);
void Start()
{
Screen.sleepTimeout = SleepTimeout.NeverSleep;
gyroEnabled = EnableGyro();
}
private bool EnableGyro()
{
if (SystemInfo.supportsGyroscope)
{
gyro = Input.gyro;
gyro.enabled = true;
Debug.Log("✅ Gyroscope enabled");
return true;
}
else
{
Debug.LogWarning("❌ Gyroscope not supported on this device.");
return false;
}
}
void Update()
{
if (!gyroEnabled) return;
Quaternion deviceRotation = gyro.attitude;
// Koreksi rotasi dari koordinat perangkat ke Unity
Quaternion correctedRotation = new Quaternion(
deviceRotation.x,
deviceRotation.y,
-deviceRotation.z,
-deviceRotation.w
);
transform.localRotation = rotationFix * correctedRotation;
}
}