Unity3D使用陀螺儀控制節(jié)點旋轉(zhuǎn)
本文實例為大家分享了Unity3D陀螺儀控制節(jié)點旋轉(zhuǎn)的具體代碼,供大家參考,具體內(nèi)容如下
/********************************************************************
Desc: 陀螺儀對相機的邏輯類。
*********************************************************************/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
namespace Game.Gyro
{
/// <summary>
/// 職責:
/// 1.實現(xiàn)陀螺儀對相機的影響和操作;
/// 2.盡量重現(xiàn)崩壞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開發(fā)環(huán)境下的模擬陀螺儀輸入 </summary>
public Vector3 m_editor_debug_input = Vector3.zero;
/// <summary> 陀螺儀的輸入?yún)?shù),用以控制相機 </summary>
public Vector3 m_gyro_input = Vector3.zero;
/// <summary> 當前的攝像機角度 </summary>
public Vector3 m_cur_euler = Vector3.zero;
/// <summary> 陀螺儀數(shù)據(jù)的更新頻率 </summary>
public int m_upate_rate = 30;
/// <summary> 當前陀螺儀的輸入輸入類型 </summary>
public EGyroInputType m_gyro_input_type = EGyroInputType.RotateRate;
/// <summary> 陀螺儀的系數(shù) </summary>
public float m_gyro_factor = 1.0f;
private Vector3 m_camera_init_euler = Vector3.zero;
private Transform mTransform;
#endregion
#region 訪問接口
/// <summary> 陀螺儀的輸入?yún)?shù),用以控制相機 </summary>
protected Vector3 GyroInput
{
get
{
return m_gyro_input;
}
set
{
m_gyro_input = value;
}
}
/// <summary> 陀螺儀輸入數(shù)據(jù)的類型 </summary>
protected EGyroInputType GyroInputType
{
get
{
return m_gyro_input_type;
}
set
{
m_gyro_input_type = value;
}
}
/// <summary> 陀螺儀的系數(shù) </summary>
protected float GyroFactor
{
get
{
return m_gyro_factor;
}
set
{
m_gyro_factor = value;
}
}
/// <summary> 當前的旋轉(zhuǎn)角 </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,方便調(diào)試 </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);
//// 陀螺儀的系數(shù)
//{
// string t_factor_str = GUI.TextField(GetRect(0.7f, 0.05f), "" + GyroFactor);
// GyroFactor = float.Parse(t_factor_str);
//}
//// 陀螺儀輸入?yún)?shù)
//{
// 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;
// 根據(jù)陀螺儀計算相機的控制數(shù)據(jù)
UpdateGyro();
// Editor下的調(diào)試
#if UNITY_EDITOR
// 開發(fā)環(huán)境下不能用陀螺儀,模擬數(shù)據(jù)
GyroInput = m_editor_debug_input;
#endif
// 因值不確定范圍,需增加系數(shù)控制
GyroInput = GyroInput * GyroFactor;
// 根據(jù)控制數(shù)據(jù),對相機進行操作和變化
UpdateCamera();
}
#endregion
#region 控制邏輯
/// <summary> 更新陀螺儀數(shù)據(jù),并計算出相應的控制數(shù)據(jù) </summary>
protected void UpdateGyro()
{
// 更新陀螺儀數(shù)據(jù),并計算出控制變量
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參數(shù)
#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 支持函數(shù)
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> 根據(jù)百分比獲取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
}
}
將腳本掛在想被陀螺儀操控的節(jié)點上就ok了。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持我們。
欄 目:C#教程
下一篇:C#中將xml文件反序列化為實例時采用基類還是派生類的知識點討論
本文標題:Unity3D使用陀螺儀控制節(jié)點旋轉(zhuǎn)
本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/4625.html
您可能感興趣的文章
- 01-10C#使用Dispose模式實現(xiàn)手動對資源的釋放
- 01-10C#3.0使用EventLog類寫Windows事件日志的方法
- 01-10C#使用windows服務開啟應用程序的方法
- 01-10c# ArrayList的使用方法小總結
- 01-10C#使用ADO.Net部件來訪問Access數(shù)據(jù)庫的方法
- 01-10C#使用Mutex簡單實現(xiàn)程序單實例運行的方法
- 01-10使用Nopcommerce為商城添加滿XX減XX優(yōu)惠券功能
- 01-10C#中yield用法使用說明
- 01-10C#編程和Visual Studio使用技巧(下)
- 01-10C#編程和Visual Studio使用技巧(上)


閱讀排行
本欄相關
- 01-10C#通過反射獲取當前工程中所有窗體并
- 01-10關于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實現(xiàn)txt定位指定行完整實例
- 01-10WinForm實現(xiàn)仿視頻播放器左下角滾動新
- 01-10C#停止線程的方法
- 01-10C#實現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實現(xiàn)讀取注冊表監(jiān)控當前操作系統(tǒng)已
隨機閱讀
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 04-02jquery與jsp,用jquery
- 01-11ajax實現(xiàn)頁面的局部加載
- 01-10SublimeText編譯C開發(fā)環(huán)境設置
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10C#中split用法實例總結
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-10delphi制作wav文件的方法


