49 lines
1.2 KiB
C#
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;
|
|
}
|
|
}
|