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.