最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Unity3D使用陀螺儀控制節(jié)點旋轉(zhuǎn)

 更新時間:2019年11月01日 17:06:40   作者:monk_CD  
這篇文章主要為大家詳細介紹了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>
 /// 職責(zé): 
 ///  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> 當(dāng)前的攝像機角度 </summary>
  public Vector3 m_cur_euler = Vector3.zero;
 
  /// <summary> 陀螺儀數(shù)據(jù)的更新頻率 </summary>
  public int m_upate_rate = 30;
 
  /// <summary> 當(dāng)前陀螺儀的輸入輸入類型 </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> 當(dāng)前的旋轉(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()
  {
   // 設(shè)置陀螺儀更新頻率
   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ù),并計算出相應(yīng)的控制數(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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C#使用Post調(diào)用接口并傳遞json參數(shù)

    C#使用Post調(diào)用接口并傳遞json參數(shù)

    這篇文章主要介紹了C#使用Post調(diào)用接口并傳遞json參數(shù),具有很好的參考價值,希望對大家有所幫助。
    2022-06-06
  • C#對XtraGrid控件實現(xiàn)主從表關(guān)系綁定

    C#對XtraGrid控件實現(xiàn)主從表關(guān)系綁定

    這篇文章介紹了C#對XtraGrid控件實現(xiàn)主從表關(guān)系綁定的方法,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-06-06
  • Base64編碼解碼原理及C#編程實例

    Base64編碼解碼原理及C#編程實例

    這篇文章主要介紹了Base64編碼解碼原理及C#編程實例,本文講解了Base64編碼由來、Base64編碼原理、C#編程實現(xiàn),需要的朋友可以參考下
    2014-10-10
  • C#窗體間通訊處理的幾種方法總結(jié)

    C#窗體間通訊處理的幾種方法總結(jié)

    這篇文章主要介紹了
    2013-11-11
  • C#透明窗體實現(xiàn)方法

    C#透明窗體實現(xiàn)方法

    這篇文章主要介紹了C#透明窗體實現(xiàn)方法,涉及C#窗體操作的相關(guān)技巧,需要的朋友可以參考下
    2015-06-06
  • C#編寫的生辰八字計算程序

    C#編寫的生辰八字計算程序

    這篇文章主要介紹了C#編寫的生辰八字計算程序,假設(shè)一個人的公歷出生時間,范圍必須要在2012-2015年之間,因為本示例程序只提供了這幾年的農(nóng)歷數(shù)據(jù),小伙伴們參考下,可以自由擴展
    2015-03-03
  • C#實現(xiàn)HSL顏色值轉(zhuǎn)換為RGB的方法

    C#實現(xiàn)HSL顏色值轉(zhuǎn)換為RGB的方法

    這篇文章主要介紹了C#實現(xiàn)HSL顏色值轉(zhuǎn)換為RGB的方法,涉及C#數(shù)值判定與轉(zhuǎn)換的相關(guān)技巧,需要的朋友可以參考下
    2015-06-06
  • C#使用密封類實現(xiàn)密封用戶信息的示例詳解

    C#使用密封類實現(xiàn)密封用戶信息的示例詳解

    在C#中,密封類(sealed class)是一種不能被其他類繼承的類,它用于防止其他類繼承它的功能和屬性, 下面我們就來看看如何使用密封類密封用戶的信息吧
    2024-02-02
  • 在C#中優(yōu)化JPEG壓縮級別和文件大小方式

    在C#中優(yōu)化JPEG壓縮級別和文件大小方式

    文章介紹了如何在C#中優(yōu)化JPEG壓縮級別和文件大小,通過使用文件菜單加載圖像文件并選擇不同的壓縮級別,程序?qū)D像保存為臨時文件并顯示生成的圖像和文件大小,關(guān)鍵方法SaveJpg使用給定的壓縮指數(shù)保存JPG文件,并通過GetEncoderInfo獲取編碼器信息
    2025-01-01
  • C#靜態(tài)構(gòu)造函數(shù)用法實例分析

    C#靜態(tài)構(gòu)造函數(shù)用法實例分析

    這篇文章主要介紹了C#靜態(tài)構(gòu)造函數(shù)用法,以實例形式較為詳細的分析了C#靜態(tài)構(gòu)造函數(shù)的用途、實現(xiàn)方法及使用技巧,需要的朋友可以參考下
    2015-06-06

最新評論

巩留县| 拉萨市| 铁岭市| 石狮市| 伊春市| 郎溪县| 和政县| 都匀市| 青田县| 东海县| 长顺县| 定襄县| 霍邱县| 贡觉县| 图们市| 盐源县| 满城县| 屯留县| 莲花县| 茂名市| 龙川县| 柘荣县| 思南县| 临清市| 浦城县| 靖宇县| 贺兰县| 祁连县| 开阳县| 天长市| 延吉市| 乌什县| 襄樊市| 固安县| 视频| 井研县| 乡城县| 开封县| 建湖县| 蒲江县| 鄂尔多斯市|