Initial commit
This commit is contained in:
48
Assets/Scripts/GyroKepala.cs
Normal file
48
Assets/Scripts/GyroKepala.cs
Normal file
@ -0,0 +1,48 @@
|
||||
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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user