Unity3D使用陀螺仪控制节点旋转
本文实例为大家分享了Unity3D陀螺仪控制节点旋转的具体代码,供大家参考,具体内容如下
/********************************************************************
Desc:陀螺仪对相机的逻辑类。
*********************************************************************/
usingSystem;
usingSystem.Collections;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingUnityEngine;
namespaceGame.Gyro
{
///
///职责:
///1.实现陀螺仪对相机的影响和操作;
///2.尽量重现崩坏3的主界面驾驶舱效果;
///
classGyroCamera:MonoBehaviour
{
#region声明
///陀螺仪的输入类型
publicenumEGyroInputType
{
///RotateRate
RotateRate,
///RotateRateUniased
RotateRateUniased,
///UserAcceleration
UserAcceleration,
}
#endregion
#region控制变量
publicfloatm_gyro_max_x=15.0f;
publicfloatm_gyro_max_y=15.0f;
publicfloatm_gyro_max_z=15.0f;
#endregion
#region变量
///editor开发环境下的模拟陀螺仪输入
publicVector3m_editor_debug_input=Vector3.zero;
///陀螺仪的输入参数,用以控制相机
publicVector3m_gyro_input=Vector3.zero;
///当前的摄像机角度
publicVector3m_cur_euler=Vector3.zero;
///陀螺仪数据的更新频率
publicintm_upate_rate=30;
///当前陀螺仪的输入输入类型
publicEGyroInputTypem_gyro_input_type=EGyroInputType.RotateRate;
///陀螺仪的系数
publicfloatm_gyro_factor=1.0f;
privateVector3m_camera_init_euler=Vector3.zero;
privateTransformmTransform;
#endregion
#region访问接口
///陀螺仪的输入参数,用以控制相机
protectedVector3GyroInput
{
get
{
returnm_gyro_input;
}
set
{
m_gyro_input=value;
}
}
///陀螺仪输入数据的类型
protectedEGyroInputTypeGyroInputType
{
get
{
returnm_gyro_input_type;
}
set
{
m_gyro_input_type=value;
}
}
///陀螺仪的系数
protectedfloatGyroFactor
{
get
{
returnm_gyro_factor;
}
set
{
m_gyro_factor=value;
}
}
///当前的旋转角
protectedVector3CurEuler
{
get
{
returnm_cur_euler;
}
set
{
m_cur_euler=value;
}
}
#endregion
#regionUnity
//Usethisforinitialization
voidStart()
{
Input.gyro.enabled=true;
mTransform=gameObject.transform;
CurEuler=mTransform.localEulerAngles;
m_camera_init_euler=CurEuler;
}
///绘制UI,方便调试
voidOnGUI()
{
//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);
////陀螺仪的系数
//{
//stringt_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)
//{
//caseEGyroInputType.RotateRate:
//GyroInputType=EGyroInputType.RotateRateUniased;
//break;
//caseEGyroInputType.RotateRateUniased:
//GyroInputType=EGyroInputType.UserAcceleration;
//break;
//caseEGyroInputType.UserAcceleration:
//GyroInputType=EGyroInputType.RotateRate;
//break;
//}
//}
//}
}
//Updateiscalledonceperframe
voidUpdate()
{
//设置陀螺仪更新频率
Input.gyro.updateInterval=1.0f/m_upate_rate;
//根据陀螺仪计算相机的控制数据
UpdateGyro();
//Editor下的调试
#ifUNITY_EDITOR
//开发环境下不能用陀螺仪,模拟数据
GyroInput=m_editor_debug_input;
#endif
//因值不确定范围,需增加系数控制
GyroInput=GyroInput*GyroFactor;
//根据控制数据,对相机进行操作和变化
UpdateCamera();
}
#endregion
#region控制逻辑
///更新陀螺仪数据,并计算出相应的控制数据
protectedvoidUpdateGyro()
{
//更新陀螺仪数据,并计算出控制变量
switch(GyroInputType)
{//手机上左倾斜x是负值,又倾斜x是正值。上倾斜y是负值,下倾斜y是正值
caseEGyroInputType.RotateRate:
GyroInput=Input.gyro.rotationRate;
break;
caseEGyroInputType.RotateRateUniased:
GyroInput=Input.gyro.rotationRateUnbiased;
break;
caseEGyroInputType.UserAcceleration:
GyroInput=Input.gyro.userAcceleration;
break;
default:
Debug.LogError("GyroInputTypeNotdefined:"+GyroInputType);
break;
}
}
///更新相机的行为
protectedvoidUpdateCamera()
{
//不需要gyro的z参数
#ifUNITY_EDITOR
Vector3t_gyro_input=newVector3(GyroInput.x,GyroInput.y,GyroInput.z);
#else
Vector3t_gyro_input=newVector3(0.0f,GyroInput.y,GyroInput.x);
#endif
CurEuler+=t_gyro_input;
//范围控制
{
floatt_x=ClampFloat(CurEuler.x,m_camera_init_euler.x,m_gyro_max_x);
floatt_y=ClampFloat(CurEuler.y,m_camera_init_euler.y,m_gyro_max_y);
floatt_z=ClampFloat(CurEuler.z,m_camera_init_euler.z,m_gyro_max_z);
CurEuler=newVector3(t_x,t_y,t_z);
}
mTransform.localEulerAngles=CurEuler;
}
#endregion
#region支持函数
protectedfloatClampFloat(floatp_float,floatp_init,floatp_offset)
{
p_offset=Mathf.Abs(p_offset);
if(p_float>p_init+p_offset)
{
p_float=p_init+p_offset;
}
if(p_float根据百分比获取gui的大概坐标
protectedRectGetRect(floatp_x_percent,floatp_y_percent,floatp_w=0.5f,floatp_h=0.1f)
{
returnnewRect(
Screen.width*p_x_percent,Screen.height*p_y_percent,
Screen.width*p_w,Screen.height*p_h);
}
#endregion
}
}
将脚本挂在想被陀螺仪操控的节点上就ok了。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。