首页 > 解决方案 > 对象跟随手指仅当它在屏幕上滑动时

问题描述

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

在此处输入图像描述

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

标签: c#unity3d

解决方案


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

你可以用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
}

推荐阅读