unity touch

//首先在start()函数里面允许多手指触屏
        void Start() { 
                 //允许多点触屏
                 Input.multiTouchEnabled=true; 
         }
//判断当前触摸屏幕的手指个数 该函数可自定义 然后在Update()里面调用 也可以直接在Update()里面写
        if(Input.touchCount <= 0) {        return;        }
        if(Input.touchCount == 1)//一个手指触摸屏幕
                {if(Input.touches[0].phase == TouchPhase.Began)//开始触屏
                        {
                                m_screenpos=Input.touches[0].position;//自定义的二维坐标向量 记录初始触屏位置
                        }else if(Input.touches[0].phase == TouchPhase.Moved)//手指移动
                        {   
                                //使物体旋转
                                this.transform.Rotate(new Vector3(-Input.touches[0].deltaPosition.y*0.5f,Input.touches[0].deltaPosition.x*0.5f,0),Space.World);
                        }

                }
        //当有多个手指触屏时 
        else if(Input.touchCount > 1)//当有多个手指触屏 
         {
                 //记录两个手指的位置         
                        Vector2 finger1= new Vector2();
                         Vector2 finger2= new Vector2();
                 //记录两个手指的移动距离 
                         Vector2 mov1=new Vector2();
                         Vector2 mov2=new Vector2();
                for (int i=0;i<2;i++)//用循环来实现记录position
                 { 
                         Touch touch = Input.touches; //记录第0个、第1个触屏点的状态
                         if(touch.phase == TouchPhase.Ended) break; //如果手指触屏之后离开就break
                         if(touch.phase == TouchPhase.Moved)// 当手指移动时
                         { 
                                 float mov =0; // 用来记录移动增量
                                 if (i == 0) {
                                         finger1=touch.position;
                                         mov1=touch.deltaPosition; 
                                 }else {
                                         finger2=touch.position;
                                         mov2=touch.deltaPosition;
                                 if(finger1.x > finger2.x){
                                                 mov=mov1.x; 
                                         }else { mov=mov2.x; }//比较两个手指在x轴上移动的距离 取其较大者
                                if(finger1.y>finger2.y)
                                                { mov+=mov1.y; }
                                        else { mov+=mov2.y; }//加上在y轴上移动的较大的增量
                                        Camera.main.transform.Translate(0,0,mov*Time.deltaTime);//移动相机 在z轴上变化 
                                         }
                                 }
                         }
                 }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值