最近在做一个2d项目,但是要配合外设来做,只需要使物体按照手机左右(y轴)的旋转来进行平面上的旋转,所以呢,之前的陀螺仪用起来就不是很方便了。
无奈之下只能舍弃之前的脚本了(附上之前的完整的代码)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 重力感应
/// </summary>
public class GyroController : MonoBehaviour {
#region [Private fields]
private bool gyroEnabled = true;
private const float lowPassFilterFactor = 0.2f;
private readonly Quaternion baseIdentity = Quaternion.Euler(90, 0, 0);
private readonly Quaternion landscapeRight = Quaternion.Euler(0, 0, 90);
private readonly Quaternion landscapeLeft = Quaternion.Euler(0, 0, -90);
private readonly Quaternion upsideDown = Quaternion.Euler(0, 0, 180);
private Quaternion cameraBase = Quaternion.identity;
private Quaternion calibration = Quaternion.identity;
private Quaternion baseOrientation = Quaternion.Euler(90, 0, 0);
private Quaternion baseOrientationRotationFix = Quaternion.identity;
private Quaternion referanceRotation = Quaternion.identity;
private bool debug = true;
#endregion
#region [Unity events]
protected void Start()
{
AttachGyro();
}
protected void Update()
{
if (!gyroEnabled)
return;
transform.rotation = Quaternion.Slerp(transform.rotat