SoFunction
Updated on 2025-03-06

Unity3D uses gyroscope to control node rotation

This article shares the specific code of Unity3D gyroscope's rotation of nodes for your reference. The specific content is as follows

/********************************************************************
  Desc: Gyroscope to camera logic class.
 ************************************************************************
 
using System;
using ;
using ;
using ;
using ;
 
using UnityEngine;
 
namespace 
{
 
 /// <summary>
 /// Responsibilities: /// 1. Realize the influence and operation of the gyroscope on the camera; /// 2. Try to reproduce the cockpit effect of the main interface of Cangfa 3; /// </summary>
 class GyroCamera : MonoBehaviour
 {
 
  #region Statement 
  /// <summary> Input type of gyroscope </summary>  public enum EGyroInputType
  {
   /// &lt;summary&gt; RotateRate &lt;/summary&gt;
   RotateRate,
 
   /// &lt;summary&gt; RotateRateUniased &lt;/summary&gt;
   RotateRateUniased,
 
   /// &lt;summary&gt; UserAcceleration &lt;/summary&gt;
   UserAcceleration,
  }
 
  #endregion
 
 
 
  #region Control variables 
  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 variable 
  /// <summary> Analog gyroscope input in editor development environment </summary>  public Vector3 m_editor_debug_input = ;
 
  /// <summary> The input parameters of the gyroscope to control the camera </summary>  public Vector3 m_gyro_input = ;
 
  /// <summary> Current camera angle </summary>  public Vector3 m_cur_euler = ;
 
  /// <summary> Frequency of update gyroscope data </summary>  public int m_upate_rate = 30;
 
  /// <summary> Current input input type of gyroscope </summary>  public EGyroInputType m_gyro_input_type = ;
 
  /// <summary> Coefficient of the gyroscope </summary>  public float m_gyro_factor = 1.0f;
 
  private Vector3 m_camera_init_euler = ;
  private Transform mTransform;
  #endregion
 
 
 
  #region Access Interface 
  /// <summary> The input parameters of the gyroscope to control the camera </summary>  protected Vector3 GyroInput
  {
   get
   {
    return m_gyro_input;
   }
   set
   {
    m_gyro_input = value;
   }
  }
 
  /// <summary> Type of gyroscope input data </summary>  protected EGyroInputType GyroInputType
  {
   get
   {
    return m_gyro_input_type;
   }
   set
   {
    m_gyro_input_type = value;
   }
  }
 
  /// <summary> Coefficient of the gyroscope </summary>  protected float GyroFactor
  {
   get
   {
    return m_gyro_factor;
   }
   set
   {
    m_gyro_factor = value;
   }
  }
 
  /// <summary> Current rotation angle </summary>  protected Vector3 CurEuler
  {
   get
   {
    return m_cur_euler;
   }
   set
   {
    m_cur_euler = value;
   }
  }
 
  #endregion
 
 
 
  #region Unity
 
  // Use this for initialization
  void Start()
  {
    = true;
 
   mTransform = ;
   CurEuler = ;
   m_camera_init_euler = CurEuler;
  }
 
  /// <summary> Draw the UI for easy debugging </summary>  void OnGUI()
  {
   //(GetRect(0.1f, 0.05f), "Attitude: " + );
 
   //(GetRect(0.1f, 0.15f), "Rotation: " + );
 
   //(GetRect(0.1f, 0.25f), "RotationUnbiased: " + );
 
   //(GetRect(0.1f, 0.35f), "UserAcceleration: " + );
 
   ///// Coefficient of the gyroscope   //{
   // string t_factor_str = (GetRect(0.7f, 0.05f), "" + GyroFactor);
 
   // GyroFactor = (t_factor_str);
   //}
 
   ///// Gyro input parameters   //{
   // if ((GetRect(0.8f, 0.8f, 0.2f), "" + GyroInputType))
   // {
   //  switch (GyroInputType)
   //  {
   //   case :
   //    GyroInputType = ;
   //    break;
 
   //   case :
   //    GyroInputType = ;
   //    break;
 
   //   case :
   //    GyroInputType = ;
   //    break;
   //  }
   // }
   //}
  }
 
  // Update is called once per frame
  void Update()
  {
   // Set the gyroscope update frequency    = 1.0f / m_upate_rate;
 
   // Calculate the camera's control data based on the gyroscope   UpdateGyro();
 
   // Debugging under Editor#if UNITY_EDITOR
   // Gyroscope cannot be used in the development environment to simulate data   GyroInput = m_editor_debug_input;
#endif
 
   // Because the value is uncertain, coefficient control needs to be added   GyroInput = GyroInput * GyroFactor;
 
   // Perform and change the camera according to the control data   UpdateCamera();
  }
 
  #endregion
 
 
 
  #region Control Logic 
  /// <summary> Update the gyroscope data and calculate the corresponding control data </summary>  protected void UpdateGyro()
  {
   // Update the gyroscope data and calculate the control variable   switch (GyroInputType)
   { //The left tilt x on the phone is negative, and the tilt x is positive.  The upper tilt y is negative, and the lower tilt y is positive    case :
     GyroInput = ;
     break;
 
    case :
     GyroInput = ;
     break;
 
    case :
     GyroInput = ;
     break;
 
    default:
     ("GyroInputTypeNot defined: " + GyroInputType);
     break;
   }
  }
 
  /// <summary> Behavior of updating camera </summary>  protected void UpdateCamera()
  {
   // gyro's z parameter is not required#if UNITY_EDITOR
   Vector3 t_gyro_input = new Vector3(, , );
#else
   Vector3 t_gyro_input = new Vector3(0.0f, , );
#endif
 
   CurEuler += t_gyro_input;
 
   // Range control   {
    float t_x = ClampFloat(, m_camera_init_euler.x, m_gyro_max_x);
 
    float t_y = ClampFloat(, m_camera_init_euler.y, m_gyro_max_y);
 
    float t_z = ClampFloat(, m_camera_init_euler.z, m_gyro_max_z);
 
    CurEuler = new Vector3(t_x, t_y, t_z);
   }
 
    = CurEuler;
  }
 
 
  #endregion
 
 
 
  #region Support functions 
  protected float ClampFloat(float p_float, float p_init, float p_offset)
  {
   p_offset = (p_offset);
 
   if (p_float &gt; p_init + p_offset)
   {
    p_float = p_init + p_offset;
   }
 
   if (p_float &lt; p_init - p_offset)
   {
    p_float = p_init - p_offset;
   }
 
   return p_float;
  }
 
  /// <summary> Obtain the approximate coordinates of the gui based on the percentage </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(
     * p_x_percent,  * p_y_percent,
     * p_w,  * p_h);
  }
 
  #endregion
 
 }
 
}

It would be OK to hang the script on the node that wants to be controlled by the gyroscope.

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.