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

本文介绍了一款游戏实现中,如何将触摸屏的屏幕坐标转换为世界坐标的方法,以便更准确地判断玩家手指滑动位置。通过Unity的Camera组件进行坐标转换,并实现了手指滑动的定速移动。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

坐标为 屏幕左下角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

 

转载于:https://www.cnblogs.com/vsdog/p/5092243.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值