设为首页收藏本站
网站公告 | 这是第一条公告
     

 找回密码
 立即注册
缓存时间18 现在时间18 缓存数据 从头到尾 我要的只有感情 可没人能给我

从头到尾 我要的只有感情 可没人能给我 -- 情深深雨濛濛

查看: 1228|回复: 5

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

[复制链接]

  离线 

TA的专栏

  • 打卡等级:热心大叔
  • 打卡总天数:239
  • 打卡月天数:0
  • 打卡总奖励:4322
  • 最近打卡:2025-08-25 09:49:16
等级头衔

等級:晓枫资讯-上等兵

在线时间
6 小时

积分成就
威望
0
贡献
440
主题
416
精华
0
金钱
5709
积分
942
注册时间
2023-1-6
最后登录
2025-9-1

发表于 2023-2-26 16:11:44 | 显示全部楼层 |阅读模式
本文实例为大家分享了Unity3D陀螺仪控制节点旋转的具体代码,供大家参考,具体内容如下
  1. /********************************************************************
  2. Desc:  陀螺仪对相机的逻辑类。
  3. *********************************************************************/

  4. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;

  9. using UnityEngine;

  10. namespace Game.Gyro
  11. {

  12. /// <summary>
  13. /// 职责:
  14. ///  1.实现陀螺仪对相机的影响和操作;
  15. ///  2.尽量重现崩坏3的主界面驾驶舱效果;
  16. /// </summary>
  17. class GyroCamera : MonoBehaviour
  18. {

  19.   #region 声明

  20.   /// <summary> 陀螺仪的输入类型 </summary>
  21.   public enum EGyroInputType
  22.   {
  23.    /// <summary> RotateRate </summary>
  24.    RotateRate,

  25.    /// <summary> RotateRateUniased </summary>
  26.    RotateRateUniased,

  27.    /// <summary> UserAcceleration </summary>
  28.    UserAcceleration,
  29.   }

  30.   #endregion



  31.   #region 控制变量

  32.   public float m_gyro_max_x = 15.0f;

  33.   public float m_gyro_max_y = 15.0f;

  34.   public float m_gyro_max_z = 15.0f;

  35.   #endregion



  36.   #region 变量

  37.   /// <summary> editor开发环境下的模拟陀螺仪输入 </summary>
  38.   public Vector3 m_editor_debug_input = Vector3.zero;

  39.   /// <summary> 陀螺仪的输入参数,用以控制相机 </summary>
  40.   public Vector3 m_gyro_input = Vector3.zero;

  41.   /// <summary> 当前的摄像机角度 </summary>
  42.   public Vector3 m_cur_euler = Vector3.zero;

  43.   /// <summary> 陀螺仪数据的更新频率 </summary>
  44.   public int m_upate_rate = 30;

  45.   /// <summary> 当前陀螺仪的输入输入类型 </summary>
  46.   public EGyroInputType m_gyro_input_type = EGyroInputType.RotateRate;

  47.   /// <summary> 陀螺仪的系数 </summary>
  48.   public float m_gyro_factor = 1.0f;

  49.   private Vector3 m_camera_init_euler = Vector3.zero;
  50.   private Transform mTransform;
  51.   #endregion



  52.   #region 访问接口

  53.   /// <summary> 陀螺仪的输入参数,用以控制相机 </summary>
  54.   protected Vector3 GyroInput
  55.   {
  56.    get
  57.    {
  58.     return m_gyro_input;
  59.    }
  60.    set
  61.    {
  62.     m_gyro_input = value;
  63.    }
  64.   }

  65.   /// <summary> 陀螺仪输入数据的类型 </summary>
  66.   protected EGyroInputType GyroInputType
  67.   {
  68.    get
  69.    {
  70.     return m_gyro_input_type;
  71.    }
  72.    set
  73.    {
  74.     m_gyro_input_type = value;
  75.    }
  76.   }

  77.   /// <summary> 陀螺仪的系数 </summary>
  78.   protected float GyroFactor
  79.   {
  80.    get
  81.    {
  82.     return m_gyro_factor;
  83.    }
  84.    set
  85.    {
  86.     m_gyro_factor = value;
  87.    }
  88.   }

  89.   /// <summary> 当前的旋转角 </summary>
  90.   protected Vector3 CurEuler
  91.   {
  92.    get
  93.    {
  94.     return m_cur_euler;
  95.    }
  96.    set
  97.    {
  98.     m_cur_euler = value;
  99.    }
  100.   }

  101.   #endregion



  102.   #region Unity

  103.   // Use this for initialization
  104.   void Start()
  105.   {
  106.    Input.gyro.enabled = true;

  107.    mTransform = gameObject.transform;
  108.    CurEuler = mTransform.localEulerAngles;
  109.    m_camera_init_euler = CurEuler;
  110.   }

  111.   /// <summary> 绘制UI,方便调试 </summary>
  112.   void OnGUI()
  113.   {
  114.    //GUI.Label(GetRect(0.1f, 0.05f), "Attitude: " + Input.gyro.attitude);

  115.    //GUI.Label(GetRect(0.1f, 0.15f), "Rotation: " + Input.gyro.rotationRate);

  116.    //GUI.Label(GetRect(0.1f, 0.25f), "RotationUnbiased: " + Input.gyro.rotationRateUnbiased);

  117.    //GUI.Label(GetRect(0.1f, 0.35f), "UserAcceleration: " + Input.gyro.userAcceleration);

  118.    //// 陀螺仪的系数
  119.    //{
  120.    // string t_factor_str = GUI.TextField(GetRect(0.7f, 0.05f), "" + GyroFactor);

  121.    // GyroFactor = float.Parse(t_factor_str);
  122.    //}

  123.    //// 陀螺仪输入参数
  124.    //{
  125.    // if (GUI.Button(GetRect(0.8f, 0.8f, 0.2f), "" + GyroInputType))
  126.    // {
  127.    //  switch (GyroInputType)
  128.    //  {
  129.    //   case EGyroInputType.RotateRate:
  130.    //    GyroInputType = EGyroInputType.RotateRateUniased;
  131.    //    break;

  132.    //   case EGyroInputType.RotateRateUniased:
  133.    //    GyroInputType = EGyroInputType.UserAcceleration;
  134.    //    break;

  135.    //   case EGyroInputType.UserAcceleration:
  136.    //    GyroInputType = EGyroInputType.RotateRate;
  137.    //    break;
  138.    //  }
  139.    // }
  140.    //}
  141.   }

  142.   // Update is called once per frame
  143.   void Update()
  144.   {
  145.    // 设置陀螺仪更新频率
  146.    Input.gyro.updateInterval = 1.0f / m_upate_rate;

  147.    // 根据陀螺仪计算相机的控制数据
  148.    UpdateGyro();

  149.    // Editor下的调试
  150. #if UNITY_EDITOR
  151.    // 开发环境下不能用陀螺仪,模拟数据
  152.    GyroInput = m_editor_debug_input;
  153. #endif

  154.    // 因值不确定范围,需增加系数控制
  155.    GyroInput = GyroInput * GyroFactor;

  156.    // 根据控制数据,对相机进行操作和变化
  157.    UpdateCamera();
  158.   }

  159.   #endregion



  160.   #region 控制逻辑

  161.   /// <summary> 更新陀螺仪数据,并计算出相应的控制数据 </summary>
  162.   protected void UpdateGyro()
  163.   {
  164.    // 更新陀螺仪数据,并计算出控制变量
  165.    switch (GyroInputType)
  166.    { //手机上左倾斜x是负值,又倾斜x是正值。上倾斜y是负值,下倾斜y是正值
  167.     case EGyroInputType.RotateRate:
  168.      GyroInput = Input.gyro.rotationRate;
  169.      break;

  170.     case EGyroInputType.RotateRateUniased:
  171.      GyroInput = Input.gyro.rotationRateUnbiased;
  172.      break;

  173.     case EGyroInputType.UserAcceleration:
  174.      GyroInput = Input.gyro.userAcceleration;
  175.      break;

  176.     default:
  177.      Debug.LogError("GyroInputTypeNot defined: " + GyroInputType);
  178.      break;
  179.    }
  180.   }

  181.   /// <summary> 更新相机的行为 </summary>
  182.   protected void UpdateCamera()
  183.   {
  184.    // 不需要gyro的z参数
  185. #if UNITY_EDITOR
  186.    Vector3 t_gyro_input = new Vector3(GyroInput.x, GyroInput.y, GyroInput.z);
  187. #else
  188.    Vector3 t_gyro_input = new Vector3(0.0f, GyroInput.y, GyroInput.x);
  189. #endif

  190.    CurEuler += t_gyro_input;

  191.    // 范围控制
  192.    {
  193.     float t_x = ClampFloat(CurEuler.x, m_camera_init_euler.x, m_gyro_max_x);

  194.     float t_y = ClampFloat(CurEuler.y, m_camera_init_euler.y, m_gyro_max_y);

  195.     float t_z = ClampFloat(CurEuler.z, m_camera_init_euler.z, m_gyro_max_z);

  196.     CurEuler = new Vector3(t_x, t_y, t_z);
  197.    }

  198.    mTransform.localEulerAngles = CurEuler;
  199.   }


  200.   #endregion



  201.   #region 支持函数

  202.   protected float ClampFloat(float p_float, float p_init, float p_offset)
  203.   {
  204.    p_offset = Mathf.Abs(p_offset);

  205.    if (p_float > p_init + p_offset)
  206.    {
  207.     p_float = p_init + p_offset;
  208.    }

  209.    if (p_float < p_init - p_offset)
  210.    {
  211.     p_float = p_init - p_offset;
  212.    }

  213.    return p_float;
  214.   }

  215.   /// <summary> 根据百分比获取gui的大概坐标 </summary>
  216.   protected Rect GetRect(float p_x_percent, float p_y_percent, float p_w = 0.5f, float p_h = 0.1f)
  217.   {
  218.    return new Rect(
  219.     Screen.width * p_x_percent, Screen.height * p_y_percent,
  220.     Screen.width * p_w, Screen.height * p_h);
  221.   }

  222.   #endregion

  223. }

  224. }
复制代码
将脚本挂在想被陀螺仪操控的节点上就ok了。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持晓枫资讯。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
晓枫资讯-科技资讯社区-免责声明
免责声明:以上内容为本网站转自其它媒体,相关信息仅为传递更多信息之目的,不代表本网观点,亦不代表本网站赞同其观点或证实其内容的真实性。
      1、注册用户在本社区发表、转载的任何作品仅代表其个人观点,不代表本社区认同其观点。
      2、管理员及版主有权在不事先通知或不经作者准许的情况下删除其在本社区所发表的文章。
      3、本社区的文章部分内容可能来源于网络,仅供大家学习与参考,如有侵权,举报反馈:点击这里给我发消息进行删除处理。
      4、本社区一切资源不代表本站立场,并不代表本站赞同其观点和对其真实性负责。
      5、以上声明内容的最终解释权归《晓枫资讯-科技资讯社区》所有。
http://bbs.yzwlo.com 晓枫资讯--游戏IT新闻资讯~~~

  离线 

TA的专栏

等级头衔

等級:晓枫资讯-列兵

在线时间
0 小时

积分成就
威望
0
贡献
0
主题
0
精华
0
金钱
17
积分
14
注册时间
2022-12-26
最后登录
2022-12-26

发表于 2023-2-28 21:12:37 | 显示全部楼层
感谢分享~~~~学习学习~~~~~
http://bbs.yzwlo.com 晓枫资讯--游戏IT新闻资讯~~~

  离线 

TA的专栏

等级头衔

等級:晓枫资讯-列兵

在线时间
0 小时

积分成就
威望
0
贡献
0
主题
0
精华
0
金钱
17
积分
14
注册时间
2022-12-29
最后登录
2022-12-29

发表于 2024-9-24 11:31:53 | 显示全部楼层
顶顶更健康!!!
http://bbs.yzwlo.com 晓枫资讯--游戏IT新闻资讯~~~

  离线 

TA的专栏

等级头衔

等級:晓枫资讯-列兵

在线时间
0 小时

积分成就
威望
0
贡献
0
主题
0
精华
0
金钱
13
积分
6
注册时间
2022-12-27
最后登录
2022-12-27

发表于 2024-10-14 16:53:49 | 显示全部楼层
路过,支持一下
http://bbs.yzwlo.com 晓枫资讯--游戏IT新闻资讯~~~

  离线 

TA的专栏

  • 打卡等级:无名新人
  • 打卡总天数:1
  • 打卡月天数:0
  • 打卡总奖励:12
  • 最近打卡:2025-09-15 10:11:21
等级头衔

等級:晓枫资讯-列兵

在线时间
0 小时

积分成就
威望
0
贡献
0
主题
0
精华
0
金钱
26
积分
4
注册时间
2024-1-21
最后登录
2025-9-15

发表于 2025-2-22 16:38:29 | 显示全部楼层
感谢楼主分享。
http://bbs.yzwlo.com 晓枫资讯--游戏IT新闻资讯~~~

  离线 

TA的专栏

  • 打卡等级:即来则安
  • 打卡总天数:29
  • 打卡月天数:0
  • 打卡总奖励:372
  • 最近打卡:2025-10-30 07:16:51
等级头衔

等級:晓枫资讯-列兵

在线时间
0 小时

积分成就
威望
0
贡献
0
主题
0
精华
0
金钱
419
积分
62
注册时间
2023-1-7
最后登录
2025-10-30

发表于 7 天前 | 显示全部楼层
感谢楼主,顶。
http://bbs.yzwlo.com 晓枫资讯--游戏IT新闻资讯~~~
严禁发布广告,淫秽、色情、赌博、暴力、凶杀、恐怖、间谍及其他违反国家法律法规的内容。!晓枫资讯-社区
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1楼
2楼
3楼
4楼
5楼
6楼

手机版|晓枫资讯--科技资讯社区 本站已运行

CopyRight © 2022-2025 晓枫资讯--科技资讯社区 ( BBS.yzwlo.com ) . All Rights Reserved .

晓枫资讯--科技资讯社区

本站内容由用户自主分享和转载自互联网,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。

如有侵权、违反国家法律政策行为,请联系我们,我们会第一时间及时清除和处理! 举报反馈邮箱:点击这里给我发消息

Powered by Discuz! X3.5

快速回复 返回顶部 返回列表