Laggy player movement on mobile

Hi guys,

I am working on my first game and everything is working fine on desktop version. On mobile I am having some issues with jagged movement.

This is the code I’m using, and the player has a kinematic rigid body 2D:

void Update()
    {
        Move();
    }

    private void Move()
    {
        if(mobileMovement)
        {
            if (Input.touchCount > 0)
            {
                Touch touch = Input.GetTouch(0);
                if (touch.phase == TouchPhase.Stationary)
                {
                    if (touch.position.x < screenWidth / 2)
                    {
                        direction = -1;
                    } else if(touch.position.x > screenWidth / 2)
                    {
                        direction = 1;
                    }

                    var newPosX = transform.position.x;
                    newPosX += direction * moveSpeed * Time.deltaTime;
                    transform.position = new Vector2(Mathf.Clamp(newPosX, xMin, xMax), transform.position.y);
                }
            }
        }
        else
        {
            var deltaX = Input.GetAxis("Horizontal") * Time.deltaTime * moveSpeed;

            var newXPos = Mathf.Clamp(transform.position.x + deltaX, xMin, xMax);
            transform.position = new Vector2(newXPos, transform.position.y);
        }
    }

What I am trying to achieve on mobile is if you tap the left half of the screen the player will move left and same for the right side. That is it. The thing is when I tap left and right side of the screen most of the time everything is ok, but sometimes player is just stuck for a moment and then continues to move. What is even more strange is that if I tap in the upper halves of the screen the issue is more prominent and happens more often. I tried disabling all objects in hierarchy except the player and the issue still occurs, so I suspect something is off with my script.

Any help is hugely appreciated.

Check for TouchPhase.Moved as well. I believe sometimes it can detect false moves

1 Like

I might be wrong, but the first thing that caught my eye is that your Move() is in Update(). By what you have described (jagged movement) it seems that your issue is due to the way your player control method is called, as Update() is called once per frame.

Try using some other form of Update() for the actions themselves, but leave the controls input in Update(). The difference is that the input is going to be recieved each frame (which makes sense), but your actual movements are not going to be “synced” with the frame updating every second.

1 Like

Hey guys, thank you both for your quick responses. @gjaccieczo that does make sense, but I added TouchPhase.Moved to my code, like @giollord suggested and that resolved the issue completely. I seems that sometimes false moves are really detected when you just tap the screen. Thank you again guys, I can’t believe it was that simple, I lost the whole day debugging it, and that solution didn’t cross my mind.

2 Likes

Glad you resolved it! Just some extra two cents, considering that you have mentioned issues during the testing of your project on mobile and no issues when on PC: FixedUpdate() runs much faster than Update(), meaning that your movement isn’t going to be as affected by FPS drops, which on mobile might be a more regular occurence than on PC due to a plethora of reasons.

Some food for thought: https://discussions.unity.com/t/475289

1 Like

@gjaccieczo thanks for elaborating, I’ll definitely look more closely into it.

1 Like