【问题标题】:Unity touch input for Rolling Sky x axis control用于 Rolling Sky x 轴控制的 Unity 触摸输入
【发布时间】:2017-04-02 14:29:08
【问题描述】:

我已经编写了一些代码,我认为这将是在 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.transform.position = new Vector3 (Input.mousePosition.x, this.transform.position.y, this.transform.position.z);

谁能帮助使第一个代码块在移动设备上正常工作?

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

移动行为(不受欢迎) https://www.youtube.com/watch?v=OooJ_NJW7V0

提前致谢

【问题讨论】:

    标签: c# unity3d mobile


    【解决方案1】:

    出于某种原因,Input.GetMouseButton 在 Android 上始终为真(不确定其他平台),因此为了强制重新定位仅在用户“按下”屏幕时发生,我使用了以下代码:

    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;
    

    【讨论】:

      【解决方案2】:

      我假设您希望球的运动更流畅,有点像游戏中的那样,而不是立即将其传送到球员触摸的地方。为此,您可以使用名为Lerp 的函数。阅读更多here

      要实现这一点,您可以替换这行代码

      this.transform.position = new Vector3 (this.transform.position.x + xMotion, this.transform.position.y, this.transform.position.z);
      

      这个

      this.transform.position = Vector3.Lerp(this.transform.position, new Vector3 (this.transform.position.x , this.transform.position.y, this.transform.position.z),Time.deltaTime)
      

      希望有帮助!

      【讨论】:

      • 感谢您的建议,我一定会实施。我仍然面临的问题是我提供的代码在桌面上完美运行,但无法在移动设备上正确执行......
      • 你在什么平台上运行它?
      • 到底是什么问题?球/球员一碰就不动?
      • 我原以为它不会移动,但奇怪的是它会移动,只是它移动就像我在我的问题中使用第二个代码块一样......
      • 所以点击任何地方它会立即捕捉到我的手指位置,而不是像在桌面上那样滚动......
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多