移动设备游戏中经常会遇到重力感应的开发,Unity简化了重力感应的开发, 通过访问Input.acceleration属性,取回加速度传感器的值。首先我们看一下重力传感器的方向问题。Unity3D中重量的取值范围是 -1.0 到 +1.0.
X轴:home按键在下手机面朝天向右旋转90度重力分量为+1.0 向左旋转90度重力分量为-1.0
Y轴:home按键在上手机背朝自己重力分量为+1.0 home按键在下手机面朝自己重力分量为-1.0
Z轴:手机面朝地面重力分量为+1.0 手机面朝天空重力分量为-1.0
为了详细说明手机移动带来的每个轴项的数值的变化,我在笔记本上面简单画了一下简图,感觉更详细一些:
如上图,手机平放到桌面xyz轴的分量是(0,0,-1),为了让其为标准化(0,0,0),需要z轴加上1来让其标准化。x轴从左到右抬起变化范围[-1,0],0是垂直水平面,然后就是[0,-1],直到手机完全覆盖到水平桌面。y和z轴的变化如上图。
tips:手机里面的坐标系如A,unity里面的坐标系如B,需要注意一下,两个坐标系是不一样的
上面分析完了直接上代码:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GyroCtrl : MonoBehaviour
{
public GameObject target; //被移动的对象
//
public float maxOffsetX = 10f; // 最大倾斜角 35
public float maxOffsetY = 10f; // 最大倾斜角 35
public float maxOffsetZ = 1f; // 最大倾斜角 35
public float posFactor = 0f; // 位移的系数
public float lerpFactor = 5f; // 差值系数
Vector3 m_MobileOrientation; //手机陀螺仪变化的值
Vector3 m_targetTransform;
Vector3 m_targetPos;
void Awake()
{
m_targetTransform = Vector3.zero;
m_targetPos = Vector3.zero;
//Input.gyro.enabled = true;
}
void Start()
{
if (target == null)
{
target = gameObject;
}
}
//运用手机的X轴的变化来改变target对象的Y轴旋转,X轴移动; Y轴的变化来改变target对象的X轴旋转,Y轴移动;Z轴的变化只去改变target对象的Z轴位置
void LateUpdate()
{
if (target == null)
return;
m_MobileOrientation = Input.acceleration;
m_targetTransform.x = Mathf.Lerp(m_targetTransform.x, m_MobileOrientation.y * maxOffsetY, Time.deltaTime * lerpFactor);
m_targetTransform.y = Mathf.Lerp(m_targetTransform.y, -m_MobileOrientation.x * maxOffsetX, Time.deltaTime * lerpFactor);
m_targetTransform.z = Mathf.Lerp(m_targetTransform.z, (-m_MobileOrientation.x) * maxOffsetZ, Time.deltaTime * lerpFactor);
m_targetPos.x = m_targetTransform.x;
m_targetPos.y = m_targetTransform.y;
m_targetPos.z = m_targetTransform.z;
target.transform.localPosition = Vector3.Lerp(target.transform.localPosition, m_targetPos * posFactor, Time.deltaTime * lerpFactor);
target.transform.localRotation = Quaternion.Euler(m_targetTransform);
//target.transform.localRotation = Quaternion.Lerp(target.transform.localRotation, Quaternion.Euler(m_targetTransform), Time.deltaTime * lerpFactor);
}
}
下面代码为策划配置系数调试用的代码,主要是开放了一些开关,让策划配置好了之后,在应用到上面的GyroCtrl .cs里面
策划配置代码我也一起贴出来,仅供参考
tips:我们当时分了七层个层,每个层的移动速度都不一样,感觉分三层,前中后,保证三层的移动速度不一样就行了
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Gyro : MonoBehaviour
{
public GameObject target; //被移动的对象
Vector3 m_MobileOrientation; //手机陀螺仪变化的值
Vector3 m_targetTransform;
Vector3 m_targetPos;
//
public float maxOffsetX = 10f; // 最大倾斜角 35
public float maxOffsetY = 10f; // 最大倾斜角 35
public float maxOffsetZ = 1f; // 最大倾斜角 35
public float posFactor = 0f; // 位移的系数
public float lerpFactor = 5f; // 差值系数
///////////////////////
private bool isGyro = true;
private Button m_btn;
private InputField m_inputField1;
private InputField m_inputField2;
private InputField m_inputField3;
private InputField m_inputField4;
private InputField m_inputField5;
void Awake()
{
m_targetTransform = Vector3.zero;
m_targetPos = Vector3.zero;
//Input.gyro.enabled = true;
}
void Start()
{
m_btn = transform.Find("Node/Button").GetComponent();
m_inputField1 = transform.Find("Node/InputField1").GetComponent();
m_inputField2 = transform.Find("Node/InputField2").GetComponent();
m_inputField3 = transform.Find("Node/InputField3").GetComponent();
m_inputField4 = transform.Find("Node/InputField4").GetComponent();
m_inputField5 = transform.Find("Node/InputField5").GetComponent();
m_btn.onClick.AddListener(OnButton);
m_inputField1.onEndEdit.AddListener(OnInputEnd1);
m_inputField2.onEndEdit.AddListener(OnInputEnd2);
m_inputField3.onEndEdit.AddListener(OnInputEnd3);
m_inputField4.onEndEdit.AddListener(OnInputEnd4);
m_inputField5.onEndEdit.AddListener(OnInputEnd5);
}
public void OnButton()
{
isGyro = !isGyro;
target.transform.localPosition = Vector3.zero;
target.transform.localRotation = Quaternion.identity;
}
public void OnInputEnd1(string strValue_)
{
maxOffsetX = Convert.ToInt32(strValue_);
}
public void OnInputEnd2(string strValue_)
{
maxOffsetY = Convert.ToInt32(strValue_);
}
public void OnInputEnd3(string strValue_)
{
maxOffsetZ = Convert.ToInt32(strValue_);
}
public void OnInputEnd4(string strValue_)
{
posFactor = Convert.ToInt32(strValue_);
}
public void OnInputEnd5(string strValue_)
{
lerpFactor = Convert.ToInt32(strValue_);
}
//运用手机的X轴的变化来改变target对象的Y轴旋转,X轴移动; Y轴的变化来改变target对象的X轴旋转,Y轴移动;Z轴的变化只去改变target对象的Z轴位置
void LateUpdate()
{
if (target == null)
return;
if (!isGyro)
return;
m_MobileOrientation = Input.acceleration;
m_targetTransform.x = Mathf.Lerp(m_targetTransform.x, m_MobileOrientation.y * maxOffsetY, Time.deltaTime * lerpFactor);
m_targetTransform.y = Mathf.Lerp(m_targetTransform.y, -m_MobileOrientation.x * maxOffsetX, Time.deltaTime * lerpFactor);
m_targetTransform.z = Mathf.Lerp(m_targetTransform.z, (-m_MobileOrientation.x) * maxOffsetZ, Time.deltaTime * lerpFactor);
m_targetPos.x = m_targetTransform.x;
m_targetPos.y = m_targetTransform.y;
m_targetPos.z = m_targetTransform.z;
target.transform.localPosition = Vector3.Lerp(target.transform.localPosition, m_targetPos * posFactor, Time.deltaTime * lerpFactor);
target.transform.localRotation = Quaternion.Euler(m_targetTransform);
//target.transform.localRotation = Quaternion.Lerp(target.transform.localRotation, Quaternion.Euler(m_targetTransform), Time.deltaTime * lerpFactor);
}
protected void OnGUI()
{
if (target == null)
return;
GUILayout.Label("Input.acceleration: " + Input.acceleration);
GUILayout.Label("Target localPosition: " + target.transform.localPosition);
GUILayout.Label("Target localRotation: " + target.transform.localRotation);
}
}
最终的游戏测试界面如下图,仅供策划调试参数用:
网友评论