用于Rolling Sky x轴控制的Unity触摸输入 [英] Unity touch input for Rolling Sky x axis control

查看:91
本文介绍了用于Rolling Sky x轴控制的Unity触摸输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一些我想成像的代码,它是在诸如《滚滚的天空》这样的游戏的玩家控制器中找到的某些东西的非常简单的版本.

I have written some code that I would image would be a very simplistic version of something found in the player controller of a game like Rolling Sky.

void Update () {
    if (Input.GetMouseButton(0)) {
        NewX = Camera.main.ScreenToViewportPoint (Input.mousePosition).x;
        float xMotion = (LastX - NewX) * -NavigationMultiplier / Screen.width;
        this.transform.position = new Vector3 (this.transform.position.x + xMotion, this.transform.position.y, this.transform.position.z);
    }
    LastX = Camera.main.ScreenToViewportPoint(Input.mousePosition).x;
}

此代码在台式机上运行良好,但正如预期的那样,在移动设备(所选平台)上的效果不佳 有趣的是,它看起来就像是玩家跟随手指一样,就像简单地使用

This code works well on desktop, but as expected, it doesn't perform so well on mobile (the platform of choice) Interestingly it just appears as if the player follows the finger similarly to simply using:

this.transform.position = new Vector3 (Input.mousePosition.x, this.transform.position.y, this.transform.position.z);

任何人都可以帮助使第一个代码块在移动设备上正常工作吗?

Can anyone help make the first code block work correctly on mobile?

桌面行为(所需) https://www.youtube.com/watch?v=PjSzEresQI8

移动行为(不需要的) https://www.youtube.com/watch?v=OooJ_NJW7V0

Mobile behavior (undesired) https://www.youtube.com/watch?v=OooJ_NJW7V0

预先感谢

推荐答案

由于某种原因,Input.GetMouseButton在Android上始终为true(不确定其他平台),因此仅当用户具有屏幕上使用以下代码:

For some reason, Input.GetMouseButton is always true on Android (not sure about other platforms), so to force the repositioning to happen only when the user has "pressed down" on the screen I used this code:

if (Input.GetTouch(0).phase == TouchPhase.Moved){
        NewX = Camera.main.ScreenToViewportPoint (Input.mousePosition).x;
        float xMotion = (LastX - NewX) * -NavigationMultiplier / Screen.width;
        this.transform.position = new Vector3 (this.transform.position.x + xMotion, this.transform.position.y, this.transform.position.z);
    }
    LastX = Camera.main.ScreenToViewportPoint(Input.mousePosition).x;

这篇关于用于Rolling Sky x轴控制的Unity触摸输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆