• 坐标转换,这次是反过来,屏幕坐标转换成世界坐标


    今天是做 安卓屏幕滑动交互的时候,发现原来安卓屏幕的坐标是屏幕坐标 

    坐标为 屏幕左下角0,0  右上角  1920*1080  是以像素为单位的。

    那么每个手机都不一样 所以如果以这个坐标来判断是左手滑动屏幕还是右手滑动屏幕会产生问题。

    而我这款游戏 摄像机是不动的  摄像机一直对着前方 摄像机的中心就是世界坐标的圆心 , 那么 我把屏幕坐标换成世界坐标就可以很方便的判断手指滑动的位置了

    所以就用到了 屏幕坐标转换到 世界坐标

    做下笔记 怕以后忘了

     1         #region 触摸屏控制  上下 射箭  移动设备
     2 
     3 
     4 
     5 
     6         if (Input.touchCount > 0)
     7         {
     8 
     9             for (int i = 0; i < Input.touchCount; i++)
    10             {
    11 
    12                 if (
    13                     Input.GetTouch(i).phase == TouchPhase.Began &&
    14                     m_camera.GetComponent<Camera>().ScreenToWorldPoint(Input.GetTouch(i).position).x > 0f &&
    15                     jian_max > 0
    16                     )//测试 只有da于零才攻击
    17                 {
    18                     //debug.text = m_camera.GetComponent<Camera>().ScreenToWorldPoint(Input.GetTouch(i).position).ToString();
    19                     Instantiate(jian,
    20                      new Vector3(PlayerTran.position.x, PlayerTran.position.y - 0.2f, PlayerTran.position.z)
    21                                 ,
    22                                    Quaternion.identity);
    23                     jian_max--;
    24                 }
    25 
    26                 else if (m_camera.GetComponent<Camera>().ScreenToWorldPoint(Input.GetTouch(i).position).x < 0f &&
    27                     Input.GetTouch(i).phase == TouchPhase.Moved)
    28                 {
    29 
    30                     //这里做移动 两种方案  手速移动和定速移动
    31 
    32                     //这是手速移动
    33                     //Vector3 word_touchPO = m_camera.GetComponent<Camera>().ScreenToWorldPoint(Input.GetTouch(i).position);
    34                     //debug.text = word_touchPO.ToString();
    35 
    36                     //player_che.transform.position = Vector3.MoveTowards(player_che.transform.position,
    37                     //    new Vector3(player_che.transform.position.x
    38                     //    , word_touchPO.y,
    39                     //    0f), 0.5f);
    40 
    41                     //下面是定速移动
    42                     //Input.GetTouch(i).
    43 
    44                     //debug.text = Input.GetTouch(i).deltaPosition.ToString();
    45                     if(Input.GetTouch(i).deltaPosition.y<0)
    46                     {
    47                         左手指状态 = -1;
    48                     }
    49                     else if(Input.GetTouch(i).deltaPosition.y>0)
    50                     {
    51                         左手指状态 = 1;
    52                     }
    53                     
    54                 }
    55                 else if(m_camera.GetComponent<Camera>().ScreenToWorldPoint(Input.GetTouch(i).position).x < 0f &&
    56                     Input.GetTouch(i).phase == TouchPhase.Ended
    57                     )
    58                 {
    59                     左手指状态 = 0;
    60                 }
    61             }
    62         }
    63         
    64 
    65         //这部分也是定速移动的一部分
    66         if(左手指状态<0&&
    67              player_che.transform.position.y > -2.92f)
    68         {
    69             player_che.transform.position = new Vector3(player_che.transform.position.x
    70                 , player_che.transform.position.y-4f*Time.deltaTime,
    71                 0f);
    72 
    73         }
    74         else if (左手指状态 > 0&&
    75             player_che.transform.position.y < 1.88f)
    76         {
    77             player_che.transform.position = new Vector3(player_che.transform.position.x
    78                 , player_che.transform.position.y + 4f * Time.deltaTime,
    79                 0f);
    80 
    81         }
    82 
    83         //定速移动完结
    84 
    85 
    86 
    87         #endregion
  • 相关阅读:
    skynet源码分析之socketchannel
    skynet源码分析之master/salve集群模式
    skynet源码分析之网络层——网关服务器
    Android反编译基础(apktoos)--广工图书馆APK
    Android-Native-Server 启动和注册详细分析
    Android NDK r8 windows环境搭建
    Android 4.1.1源码编译
    Android Binder------ServiceManager启动分析
    更改系统盘符后DFS无法复制故障处理
    Centos6.9部署Gitlab-11.9.8并汉化
  • 原文地址:https://www.cnblogs.com/vsdog/p/5092243.html
Copyright © 2020-2023  润新知