欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Unity3D使用陀螺仪控制节点旋转

程序员文章站 2023-11-09 18:48:46
本文实例为大家分享了unity3d陀螺仪控制节点旋转的具体代码,供大家参考,具体内容如下 /***************************************...

本文实例为大家分享了unity3d陀螺仪控制节点旋转的具体代码,供大家参考,具体内容如下

/********************************************************************
 desc:  陀螺仪对相机的逻辑类。
*********************************************************************/
 
using system;
using system.collections;
using system.collections.generic;
using system.linq;
using system.text;
 
using unityengine;
 
namespace game.gyro
{
 
 /// <summary>
 /// 职责: 
 ///  1.实现陀螺仪对相机的影响和操作;
 ///  2.尽量重现崩坏3的主界面驾驶舱效果;
 /// </summary>
 class gyrocamera : monobehaviour
 {
 
  #region 声明
 
  /// <summary> 陀螺仪的输入类型 </summary>
  public enum egyroinputtype
  {
   /// <summary> rotaterate </summary>
   rotaterate,
 
   /// <summary> rotaterateuniased </summary>
   rotaterateuniased,
 
   /// <summary> useracceleration </summary>
   useracceleration,
  }
 
  #endregion
 
 
 
  #region 控制变量
 
  public float m_gyro_max_x = 15.0f;
 
  public float m_gyro_max_y = 15.0f;
 
  public float m_gyro_max_z = 15.0f;
 
  #endregion
 
 
 
  #region 变量
 
  /// <summary> editor开发环境下的模拟陀螺仪输入 </summary>
  public vector3 m_editor_debug_input = vector3.zero;
 
  /// <summary> 陀螺仪的输入参数,用以控制相机 </summary>
  public vector3 m_gyro_input = vector3.zero;
 
  /// <summary> 当前的摄像机角度 </summary>
  public vector3 m_cur_euler = vector3.zero;
 
  /// <summary> 陀螺仪数据的更新频率 </summary>
  public int m_upate_rate = 30;
 
  /// <summary> 当前陀螺仪的输入输入类型 </summary>
  public egyroinputtype m_gyro_input_type = egyroinputtype.rotaterate;
 
  /// <summary> 陀螺仪的系数 </summary>
  public float m_gyro_factor = 1.0f;
 
  private vector3 m_camera_init_euler = vector3.zero;
  private transform mtransform;
  #endregion
 
 
 
  #region 访问接口
 
  /// <summary> 陀螺仪的输入参数,用以控制相机 </summary>
  protected vector3 gyroinput
  {
   get
   {
    return m_gyro_input;
   }
   set
   {
    m_gyro_input = value;
   }
  }
 
  /// <summary> 陀螺仪输入数据的类型 </summary>
  protected egyroinputtype gyroinputtype
  {
   get
   {
    return m_gyro_input_type;
   }
   set
   {
    m_gyro_input_type = value;
   }
  }
 
  /// <summary> 陀螺仪的系数 </summary>
  protected float gyrofactor
  {
   get
   {
    return m_gyro_factor;
   }
   set
   {
    m_gyro_factor = value;
   }
  }
 
  /// <summary> 当前的旋转角 </summary>
  protected vector3 cureuler
  {
   get
   {
    return m_cur_euler;
   }
   set
   {
    m_cur_euler = value;
   }
  }
 
  #endregion
 
 
 
  #region unity
 
  // use this for initialization
  void start()
  {
   input.gyro.enabled = true;
 
   mtransform = gameobject.transform;
   cureuler = mtransform.localeulerangles;
   m_camera_init_euler = cureuler;
  }
 
  /// <summary> 绘制ui,方便调试 </summary>
  void ongui()
  {
   //gui.label(getrect(0.1f, 0.05f), "attitude: " + input.gyro.attitude);
 
   //gui.label(getrect(0.1f, 0.15f), "rotation: " + input.gyro.rotationrate);
 
   //gui.label(getrect(0.1f, 0.25f), "rotationunbiased: " + input.gyro.rotationrateunbiased);
 
   //gui.label(getrect(0.1f, 0.35f), "useracceleration: " + input.gyro.useracceleration);
 
   //// 陀螺仪的系数
   //{
   // string t_factor_str = gui.textfield(getrect(0.7f, 0.05f), "" + gyrofactor);
 
   // gyrofactor = float.parse(t_factor_str);
   //}
 
   //// 陀螺仪输入参数
   //{
   // if (gui.button(getrect(0.8f, 0.8f, 0.2f), "" + gyroinputtype))
   // {
   //  switch (gyroinputtype)
   //  {
   //   case egyroinputtype.rotaterate:
   //    gyroinputtype = egyroinputtype.rotaterateuniased;
   //    break;
 
   //   case egyroinputtype.rotaterateuniased:
   //    gyroinputtype = egyroinputtype.useracceleration;
   //    break;
 
   //   case egyroinputtype.useracceleration:
   //    gyroinputtype = egyroinputtype.rotaterate;
   //    break;
   //  }
   // }
   //}
  }
 
  // update is called once per frame
  void update()
  {
   // 设置陀螺仪更新频率
   input.gyro.updateinterval = 1.0f / m_upate_rate;
 
   // 根据陀螺仪计算相机的控制数据
   updategyro();
 
   // editor下的调试
#if unity_editor
   // 开发环境下不能用陀螺仪,模拟数据
   gyroinput = m_editor_debug_input;
#endif
 
   // 因值不确定范围,需增加系数控制
   gyroinput = gyroinput * gyrofactor;
 
   // 根据控制数据,对相机进行操作和变化
   updatecamera();
  }
 
  #endregion
 
 
 
  #region 控制逻辑
 
  /// <summary> 更新陀螺仪数据,并计算出相应的控制数据 </summary>
  protected void updategyro()
  {
   // 更新陀螺仪数据,并计算出控制变量
   switch (gyroinputtype)
   { //手机上左倾斜x是负值,又倾斜x是正值。上倾斜y是负值,下倾斜y是正值
    case egyroinputtype.rotaterate:
     gyroinput = input.gyro.rotationrate;
     break;
 
    case egyroinputtype.rotaterateuniased:
     gyroinput = input.gyro.rotationrateunbiased;
     break;
 
    case egyroinputtype.useracceleration:
     gyroinput = input.gyro.useracceleration;
     break;
 
    default:
     debug.logerror("gyroinputtypenot defined: " + gyroinputtype);
     break;
   }
  }
 
  /// <summary> 更新相机的行为 </summary>
  protected void updatecamera()
  {
   // 不需要gyro的z参数
#if unity_editor
   vector3 t_gyro_input = new vector3(gyroinput.x, gyroinput.y, gyroinput.z);
#else
   vector3 t_gyro_input = new vector3(0.0f, gyroinput.y, gyroinput.x);
#endif
 
   cureuler += t_gyro_input;
 
   // 范围控制
   {
    float t_x = clampfloat(cureuler.x, m_camera_init_euler.x, m_gyro_max_x);
 
    float t_y = clampfloat(cureuler.y, m_camera_init_euler.y, m_gyro_max_y);
 
    float t_z = clampfloat(cureuler.z, m_camera_init_euler.z, m_gyro_max_z);
 
    cureuler = new vector3(t_x, t_y, t_z);
   }
 
   mtransform.localeulerangles = cureuler;
  }
 
 
  #endregion
 
 
 
  #region 支持函数
 
  protected float clampfloat(float p_float, float p_init, float p_offset)
  {
   p_offset = mathf.abs(p_offset);
 
   if (p_float > p_init + p_offset)
   {
    p_float = p_init + p_offset;
   }
 
   if (p_float < p_init - p_offset)
   {
    p_float = p_init - p_offset;
   }
 
   return p_float;
  }
 
  /// <summary> 根据百分比获取gui的大概坐标 </summary>
  protected rect getrect(float p_x_percent, float p_y_percent, float p_w = 0.5f, float p_h = 0.1f)
  {
   return new rect(
    screen.width * p_x_percent, screen.height * p_y_percent,
    screen.width * p_w, screen.height * p_h);
  }
 
  #endregion
 
 }
 
}

将脚本挂在想被陀螺仪操控的节点上就ok了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。