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了。

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

(0)

相关推荐

  • Unity实现旋转扭曲图像特效

    旋转扭曲特效是指在一个圆形区域内扭曲所渲染的图像,其他像素的旋转程度随着距离的变化而变化.具体可以通过修改Shader来实现. 原始图片 扭曲图片 /*==================================================== 屏幕扭曲特效Shader ======================================================*/ Shader "Hidden/TwirlEffects" { Properties { _M

  • Unity实现人物旋转和移动效果

    本文实例为大家分享了Unity实现人物旋转和移动的具体代码,供大家参考,具体内容如下 旋转 using System.Collections; using System.Collections.Generic; using UnityEngine; public class MouseLook : MonoBehaviour { public enum RotationAxes { MouseXandY = 0, MouseX = 1, MouseY = 2 } public RotationA

  • Unity向量按照某一点进行旋转

    本文实例为大家分享了Unity向量按照某一点进行旋转的具体代码,供大家参考,具体内容如下 一.unity的旋转 首先要知道一点就是在Unity的旋转中使用过四元数进行旋转的,如果对一个物体的rotation直接赋值你会发现结果不是你最终想要的结果,这个时候我们需要去借助Quaternion来进行旋转. 二.向量按照原点进行旋转 用到的Unity内置方法Quaternion.AngleAxis(float angle,Vector3 axis) 第一个参数就是我们需要旋转的角度 angle大于0时

  • Unity实现物体沿自身的任意轴向旋转

    本文实例为大家分享了Unity实现物体沿任意轴向旋转,供大家参考,具体内容如下 一.创建一个需要旋转的物体 二.编写控制该物体的脚本 using UnityEngine; using System.Collections; public class Test_ElectricFan : MonoBehaviour { public bool isOpen=false; //是否开始旋转 public int speed=2; //旋转的速度 // Use this for initializat

  • Unity3D实现鼠标控制旋转转盘

    在培训模拟考试软件中,我们经常会遇到类似用鼠标旋转转盘打开开关的需求.让用户更加真实的操作设备仪器.接下来说下我的解决方案. 因为鼠标操作是在UI平面上,所以我们要先将被操作的模型的世界坐标转换到屏幕坐标上.代码如下: ModelScreenPos = camera.WorldToScreenPoint(Model.transform.position); 这里有个声明,这个模型代表的是转盘,而且要保证模型的中心点在转盘中心.然后我们就要计算鼠标以模型在屏幕坐标为中心点的旋转偏移量.我们开始以鼠

  • Unity3D使用鼠标旋转缩放平移视角

    Unity使用鼠标旋转缩放平移视角,供大家参考,具体内容如下 用代码在Game界面完美实现Scene界面的操作方法. 使用方法:把脚本挂在相机上,把跟踪的target拖到脚本上. 视角跟踪的是一个空物体,当然如果你是做RPG游戏需要跟踪某一角色的视角,那就不需要中键平移功能,把空物体换成角色就行. 代码主要是分三部分功能进行实现. 1.右键拖动控制视角的旋转: 2.滚轮旋转控制视角的缩放: 3.中键拖动控制视角的平移. 右键拖动控制旋转主要是用GetAxis获得鼠标在x方向与y方向平移的距离,相

  • Unity3D实现物体旋转缩放移动效果

    本文实例为大家分享了Unity3D实现物体旋转缩放移动的具体代码,供大家参考,具体内容如下 由于项目运行在安卓上,运用到了插件,比较麻烦.你们可以在触发条件上进行修改,不用插件也可以. 1.下载FingerGestures 插件 下载地址 点击打开链接 2.导入插件,创建场景 将预设Finger Gestures Initializer 拖拽到 Hierarchy 视图中 3.添加脚本,拖拽到摄像机上.创建一个方块拖拽到脚本target 属性上. using UnityEngine; using

  • Unity实现鼠标点2D转3D进行旋转

    本文实例为大家分享了Unity实现鼠标点2D转3D进行旋转的具体代码,供大家参考,具体内容如下 代码如下: using UnityEngine; public class GunFollowMouse : MonoBehaviour { public RectTransform UGUICanvas; public Camera mainCamera; //摄像机旋转的缓动速率 private float rotateSpeed = 5; void Start () { } void Updat

  • Unity控制指针旋转到指定位置

    本文实例为大家分享了Unity控制指针旋转到指定位置的具体代码,供大家参考,具体内容如下 一.搭建基础的表盘.指针 二.编写控制指针旋转到指定位置的脚本: using UnityEngine; using System.Collections; public class Test_OnDashboard : MonoBehaviour { public int thiAngle = 0; public int rotateSpeed = 2; public bool openRotate = f

  • Unity实现绕任意轴任意角度旋转向量

    本文实例为大家分享了Unity实现绕任意轴任意角度旋转向量的具体代码,供大家参考,具体内容如下 游戏中有一需求,就是一个矩形或者Cube绕着某一点旋转任意角度,现在给出下面算法. public static Vector3 RotateRound(Vector3 position, Vector3 center, Vector3 axis, float angle) { Vector3 point = Quaternion.AngleAxis(angle, axis) * (position -

随机推荐