unity加速传感器的应用
来源:互联网 发布:js 数组的join方法 编辑:程序博客网 时间:2024/05/04 18:21
1、加速传感器 ??? 在塞车类游戏中,通过移动设备的左右倾斜来模拟游戏中的方向盘,这就用到了加速传感器。可以开发跑酷类游戏。
2、基础知识:
线性加速度三维向量x,y,z分别标识手机屏幕竖直、水平、垂直方向。通过手机重力传感器就能获取手机移动或旋转过程中的3个分量,使用时在代码中调用Input.acceleration方法即可。
3、案例介绍: 倾斜手机,移动小球,当小球到达光圈位置时,传送到另一个光圈的位置(类似LOL里的传送),如下图1,2所示。
图1
图2
开发流程,
1、搭建场景:BlueClinderFX为粒子特效,其他都是用plane,cube,sphere进行搭建。如图3所示。
图3
2、场景创建完成后,利用加速传感器,操控手机控制小球的移动。
UpdateInput()函数作用是通过鼠标空盒子小球移动,便于在电脑上操控小球。
flag标志位,相当于一个开关,控制特效的显示和隐藏。
Invoke 为一定时间后执行某个方法。
dir.x = Input.acceleration.x; 三维向量的x分量为加速度传感器的x分量
dir.z = Input.acceleration.y; 三维向量的z分量为加速度传感器的y分量
完整代码如下:
using UnityEngine;using System.Collections;public class Control : MonoBehaviour{ public Transform destroy; //声明挂载BlueClinderFX游戏对象的变量 public Transform flash; //声明挂载BlueClinderFX(1)游戏对象的变量 public Transform sphere; //声明场景中小球的游戏变量 Vector3 dir = Vector3.zero; //声明一个三维向量的变量 private float distance; //定义距离变量 private bool flag=false; //声明一个用来判断小球是否消失的标志位 private float mindistance = 2.0f; //定义小球和BlueClinderFX游戏对象的最小距离变量 private Vector3 currentPos; private float speed = 5.0f; void Update(){ UpdateInput(); dir.x = Input.acceleration.x; //三维向量的x分量为加速度传感器的x分量 dir.z = Input.acceleration.y; //三维向量的z分量为加速度传感器的y分量 this.transform.GetComponent<Rigidbody>().AddForce(dir*5);//为小球添加力的效果 distance = Vector3.Distance(sphere.position, destroy.position);//获取当前小球和BlueClinderFX的距离 if(distance <= mindistance) { sphere.position = destroy.position;//重置小球的当前位置 Invoke("spheredestroy", 0.1f); //在0.1秒后调用spheredestroy方法 flag = !flag; //标志位置反 } if(flag){ //destroy.gameObject.SetActive(true);//BlueClinderFX sphere.position = flash.position;//重置小球的当前位置 flash.gameObject.SetActive(true);//将BlueClinderFX(1)游戏对象的active置为true Invoke("sphereflash", 1.0f);//在1秒后调用sphereflash方法 Invoke("flashreset", 10.0f);//在1秒后调用flashreset方法 flag = !flag;//标志位置反 } } void spheredestroy(){ sphere.gameObject.SetActive(false);//将小球的active置为false(即为不可见) } void sphereflash(){ sphere.gameObject.SetActive(true);//将小球的active置为true } void flashreset(){ flash.gameObject.SetActive(false);//将BlueClinderFX(1)对象的active置为false } void UpdateInput() { if (Input.GetMouseButton(0)) { Vector3 ms = Input.mousePosition; // ms = Camera.main.ScreenToWorldPoint(ms); Ray ray = Camera.main.ScreenPointToRay(ms); RaycastHit hit; if (Physics.Raycast(ray, out hit)) { currentPos = hit.point; } Vector3 pos = Vector3.MoveTowards(this.transform.position, currentPos, speed * Time.deltaTime); this.transform.position = pos; } }}
0 0
- unity加速传感器的应用
- Android——加速传感器(ACCELEROMETER)的应用
- android 加速的传感器(重力传感器)
- 加速传感器
- 传感器之加速传感器
- 传感器的应用
- Android的传感器应用
- 磁性传感器的应用
- Android传感器的应用
- 重力加速传感器 mma7660
- 重力加速传感器 mma7660
- ios 加速传感器UIAccelerometer
- libgdx 处理加速传感器
- android---加速传感器
- swift 加速传感器
- 简单的获取加速传感器数据的例子
- 数字传感器:新兴的应用
- android距离传感器的应用
- 编程修养
- 如何让控件只有一个圆角
- jquery改变标签背景
- Ubuntu下借助Qt Creator 调试Caffe
- 1.java学习 正则表达式学习 (Pattern Matches)
- unity加速传感器的应用
- Android 自定义view - 随机验证码
- 需要定义一个变量数组,但是C++ 数组无法用变量初始化,解决方案
- 新手必备的常用代码片段整理(一)
- 流场寻路算法学习及实现
- activiti5.20简单介绍(七) -- 流程操作
- java设计模式,责任链模式
- 如何在uboot中添加驱动程序
- 前端构建工具、browserify、gulp的组合使用及尝试感言