繁体   English   中英

为什么我的触摸屏输入不能在Android上使用?

[英]Why isn't my touch screen input working on Android?

我想用触摸输入制作一个小游戏,这是我为此编写的代码:(这在Update方法内部)

//Check if Input has registered more than zero touches
            if(Input.touchCount > 0){
                //Store the first touch detected.
                Touch myTouch = Input.touches[0];
                //Check if the phase of that touch equals Began
                if (myTouch.phase == TouchPhase.Began)
                {
                    //If so, set touchOrigin to the position of that touch
                    touchOrigin = myTouch.position;

                    if(touchOrigin.x < -2){
                        horizontalInput = -1;
                    } else if(touchOrigin.x > 2){
                        horizontalInput = 1;
                    }
                    if(touchOrigin.x > -2 && touchOrigin.x < 2 && touchOrigin.y < 0){
                        verticalInput = 1;
                    }
                } else if(myTouch.phase == TouchPhase.Ended){
                    horizontalInput = 0;
                    verticalInput = 0;
                }

            }

这是我的场景: http : //i.stack.imgur.com/PGbd3.jpg当我在Android手机上尝试时,它仅移至右侧,我不知道为什么。 (我的相机的位置是:x:-6,6; y:-2,6)而我使用的是正交相机

这是在您使用Touch.position的情况下,该函数返回基于SCREEN空间而不是WORLD空间的值。

您需要做的是在应用条件之前将点从“屏幕”转换为“世界”空间。 您可以使用Camera.ScreenToWorldPoint做到这一点

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM