最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Unity 2D sprite not moving exactly along with finger touch in mobile game - Stack Overflow

programmeradmin0浏览0评论

I have a sprite in a 2d mobile game in unity and I am trying to touch and drag that sprite with my finger so the sprite should move exactly where the finger is touched on the screen. But the problem is the sprite always lags behind where the finger is on the screen, when I move the finger at very fast speeds, the sprite is delayed. The sprite shall be very immediate in its movement and be present at the point on screen wherever the finger is touched (the sprite will be dragged by the finger touch) at all times, it is very crucial for the game.

I have tried below code:

void Update()
{
    if (Input.touchCount > 0)
    {
        Touch touch = Input.GetTouch(0);
        Vector3 touchPosition = Camera.main.ScreenToWorldPoint(touch.position);
        touchPosition.z = 0f;
        if (touch.phase == TouchPhase.Began)
        {
            touchOffset = transform.position - touchPosition;
        }
        transform.position = touchPosition + touchOffset;
    }
}

With this, the sprite has a delay at very fast speeds of finger touch and drag. The speed is very crucial for the game and the sprite should always be present where the finger is touched on the screen.

发布评论

评论列表(0)

  1. 暂无评论