unity加速传感器的应用

1、加速传感器 ??? 在塞车类游戏中,通过移动设备的左右倾斜来模拟游戏中的方向盘,这就用到了加速传感器。可以开发跑酷类游戏。
2、基础知识:
线性加速度三维向量x,y,z分别标识手机屏幕竖直、水平、垂直方向。通过手机重力传感器就能获取手机移动或旋转过程中的3个分量,使用时在代码中调用Input.acceleration方法即可。
3、案例介绍: 倾斜手机,移动小球,当小球到达光圈位置时,传送到另一个光圈的位置(类似LOL里的传送),如下图1,2所示。
图1
图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
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值