【问题标题】:Object Follow finger Only when it swipe on the Screen对象跟随手指仅当它在屏幕上滑动时
【发布时间】:2018-08-17 22:37:31
【问题描述】:

我有一个球体正在向前移动,当它在屏幕上滑动时我需要它平稳地跟随我的手指,但只能在 x 轴上。 我试图做出像Rolling SkyColor Road这样的确切动作

当球在 x 轴上滑动时应该跟随我的手指并平稳移动。 就像我之前提到的游戏一样。 我尝试了OnMouseDrag 和很多方法,但它不能正常工作,因为它不跟随手指或者在手指滑动时它不移动。

【问题讨论】:

  • 您能否提供更多详细信息,说明您尝试过的方法以及确切的问题是什么?
  • 好的,我要编辑问题
  • 尝试最初将 x 位置设置为 Input.mousePosition.x?
  • 给我们看一些代码...

标签: c# unity3d


【解决方案1】:

据我了解,您似乎想获取用户手指的位置并相应地移动球?

您可以通过Touch.position 实现此目的

例子:

private void Update()
{
    Touch touch = Input.GetTouch(0); // Get the touch data for the first finger
    Vector2 position = touch.position; // Get the position in screen-space
    Vector3 worldPosition = Camera.main.ScreenToWorldPoint(position); // Convert the position to world space

    Vector3 ballPosition = ball.transform.position;
    ball.transform.position = new Vector3(worldPosition.x, ballPosition.y, ballPosition.z); // Move the ball
}

【讨论】:

  • 我必须把这个脚本放在球体上吗?
  • 如果你愿意,你可以把它放在球体上。您只需将ball.transform 更改为transform
【解决方案2】:

我认为您应该将输入与球体分开处理。

把它放在一个脚本上来检测屏幕上的滑动。

    bool inputBegan = false;
    Vector2 beganPosition;

    void Update()
    {
        if (!inputBegan && Input.GetMouseButtonDown (0))
        {
            inputBegan = true;
            beganPosition = Input.mousePosition;
        }
        else if (inputBegan && Input.GetMouseButtonUp (0))
        {
            //If you want a threshold you need to change this comparison
            if (beganPosition.x > Input.mousePosition) 
            {
                MoveBallLeft ();
            }
            else
            {
                MoveBallRight ();
            }
            inputBegan = false;
        }
    }

球的运动你可以通过添加或减去Vector2.right * movingDistance 到球体 变换如果你想传送或使用targetPositioncurrentPosition 使球体缓慢移动并且每个更新周期添加一点一点变换距离。

【讨论】:

    【解决方案3】:

    我通过 this 解决了它并将变量 N​​avigationMultiplier 设置为 10000

    【讨论】:

      猜你喜欢
      • 2011-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多