服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - Unity3D使用陀螺仪控制节点旋转

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

2022-08-08 10:29monk_CD C#

这篇文章主要为大家详细介绍了Unity3D使用陀螺仪控制节点旋转,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/********************************************************************
 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了。

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

原文链接:https://blog.csdn.net/qweewqpkn/article/details/79758732

延伸 · 阅读

精彩推荐
  • C#C#访问及调用类中私有成员与方法示例代码

    C#访问及调用类中私有成员与方法示例代码

    访问一个类的私有成员不是什么好做法,大家也都知道私有成员在外部是不能被访问的,这篇文章主要给大家介绍了关于C#访问及调用类中私有成员与方法...

    cnc4722022-02-24
  • C#c# 类成员的可访问性代码详解

    c# 类成员的可访问性代码详解

    在本篇文章里小编给大家整理了关于c# 类成员的可访问性代码详解内容,有需要的朋友们可以参考下。...

    樊伟胜7182022-08-04
  • C#C#实现的SQL备份与还原功能示例

    C#实现的SQL备份与还原功能示例

    这篇文章主要介绍了C#实现的SQL备份与还原功能,结合具体实例形式分析了C#操作数据库实现SQL备份与还原相关的控件、SQL连接、文件等操作技巧,需要的朋友...

    a7719485246442022-01-12
  • C#C#程序员统计自己的代码行数

    C#程序员统计自己的代码行数

    这篇文章给大家讲解了下作为程序员如何统计自己写过的代码的行数,这个也是证明自己程序员能力的一个表现,一起来看下。...

    昆明--菜鸟入门9242022-02-15
  • C#C#打开php链接传参然后接收返回值的关键代码

    C#打开php链接传参然后接收返回值的关键代码

    这篇文章主要介绍了C#打开php链接传参然后接收返回值的关键代码,代码简单易懂,非常不错,具有参考借鉴价值,需要的朋友可以参考下...

    7482021-12-03
  • C#C#使用UdpClient类进行简单通信的实例

    C#使用UdpClient类进行简单通信的实例

    本文主要介绍了C#使用UdpClient类进行简单通信的实例,具有很好的参考价值,需要的朋友可以看下...

    弎吩锺熱℃9152021-12-14
  • C#C#使用doggleReport生成pdf报表的方法

    C#使用doggleReport生成pdf报表的方法

    这篇文章主要介绍了C#使用doggleReport生成pdf报表的方法,结合实例形式分析了C# doggleReport安装及使用具体操作技巧,需要的朋友可以参考下...

    _iorilan4432022-01-11
  • C#C#数据结构之堆栈(Stack)实例详解

    C#数据结构之堆栈(Stack)实例详解

    这篇文章主要介绍了C#数据结构之堆栈(Stack),结合实例形式较为详细的分析了堆栈的原理与C#实现堆栈功能的相关技巧,具有一定参考借鉴价值,需要的朋友可...

    Jimmy.Yang10132021-11-04