03_Unity3D的输入(Input)——移动设备重力感应
2014-11-24 21:00
411 查看
移动设备游戏中经常会遇到重力感应的开发,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
方向如下图所示。
![](https://img-blog.csdn.net/20141124210135656)
unity中使用Input.acceleration的x,y,z属性即可获得重力分量:
Input.acceleration.x; 重力感应X轴的重力分量
Input.acceleration.y; 重力感应Y轴的重力分量
Input.acceleration.z; 重力感应Z轴的重力分量
下面我们使用脚本来掩饰重力感应效果,分别控制贴图移动和方块移动。
![](https://img-blog.csdn.net/20141124210211578)
public class mono4 : MonoBehaviour {
public Texture texture;//贴图,在Inspector中指定
private float x,y;//贴图的坐标
public float speed = 10.0F;//方块移动速度
void Update() {
//以下控制方块移动
Vector3 dir = Vector3.zero;//方块移动向量
dir.x = -Input.acceleration.y;
dir.z = Input.acceleration.x;
if (dir.sqrMagnitude > 1)
dir.Normalize();
dir *= Time.deltaTime;
transform.Translate(dir * speed);
//以下控制贴图移动
x += Input.acceleration.x * 30;
y += -Input.acceleration.y * 30;
//避免小球超出屏幕
if(x < 0){
x = 0;
}else if(x > Screen.width-50){
x = Screen.width-50;
}
if(y < 0){
y = 0;
}else if(y > Screen.height-50){
y = Screen.height-50;
}
}
void OnGUI(){
//将重力分量打印出来
GUI.Label(new Rect(100,100,100,100),"x="+Input.acceleration.x+" y="+Input.acceleration.y+" z="+Input.acceleration.z);
//绘制贴图
GUI.DrawTexture(new Rect(x,y,50,50),texture);
}
}
在Android真机上测试如下:
通过访问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
方向如下图所示。
unity中使用Input.acceleration的x,y,z属性即可获得重力分量:
Input.acceleration.x; 重力感应X轴的重力分量
Input.acceleration.y; 重力感应Y轴的重力分量
Input.acceleration.z; 重力感应Z轴的重力分量
下面我们使用脚本来掩饰重力感应效果,分别控制贴图移动和方块移动。
public class mono4 : MonoBehaviour {
public Texture texture;//贴图,在Inspector中指定
private float x,y;//贴图的坐标
public float speed = 10.0F;//方块移动速度
void Update() {
//以下控制方块移动
Vector3 dir = Vector3.zero;//方块移动向量
dir.x = -Input.acceleration.y;
dir.z = Input.acceleration.x;
if (dir.sqrMagnitude > 1)
dir.Normalize();
dir *= Time.deltaTime;
transform.Translate(dir * speed);
//以下控制贴图移动
x += Input.acceleration.x * 30;
y += -Input.acceleration.y * 30;
//避免小球超出屏幕
if(x < 0){
x = 0;
}else if(x > Screen.width-50){
x = Screen.width-50;
}
if(y < 0){
y = 0;
}else if(y > Screen.height-50){
y = Screen.height-50;
}
}
void OnGUI(){
//将重力分量打印出来
GUI.Label(new Rect(100,100,100,100),"x="+Input.acceleration.x+" y="+Input.acceleration.y+" z="+Input.acceleration.z);
//绘制贴图
GUI.DrawTexture(new Rect(x,y,50,50),texture);
}
}
在Android真机上测试如下:
相关文章推荐
- 02_Unity3D的输入(Input)——移动设备输入
- 04_Unity3D的输入(Input)——移动设备方向
- Linux输入子系统:输入设备编程指南 -- input-programming.txt
- Android input输入设备getevent 和sendevent用法
- Unity3D之Input输入事件总结
- Unity3D的Android移动之路之辅助工具及设备姿态
- Linux的input输入子系统:设备驱动之按键驱动
- [Unity3D]控制输入系统 - Input 类实例讲解
- linux 读取input输入设备demo
- linux设备驱动之输入(input)子系统——数据结构与接口介绍
- Unity3D笔记第二十四天—— 移动平台输入
- 我的和unity3d的小故事1——恶魔射手之鼠标控制移动之input.getaxis("Horizontal")与input.getaxis("Vertical")
- Unity3D播放视频(包括在IOS和Android移动设备中播放视频)
- unity3d移动平台性能优化专题(4):为不同性能的设备设置不同的性能级别
- 【转】unity3d input输入
- Unity--移动设备重力感应探悉
- 输入设备驱动(input子系统)
- linux设备驱动之输入(input)子系统——核心层实现
- UNITY3d在移动设备上的一些优化实战(一)-概述
- unity3d中用incontrol检测输入设备切换