- Home /
EventSystem micro lag on the first touch
Hey! I have been writing a project and at one moment there was a problem. So I build my game for mobile devices. If I touch the screen there is micro lag and after it game works perfect. This lag takes milliseconds, but is's really annoying. I had spent lots of time to understand the reason of this proublem. So I created a new simple project with three objects on my scene: player, floor and camera. Then I tried to move the player using two simple scripts:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
private Rigidbody2D RB;
public float speed;
void Start()
{
RB = GetComponent<Rigidbody2D>();
}
void Update()
{
DetectInput();
}
public void MoveLeft()
{
RB.velocity = new Vector2(-speed, RB.velocity.y);
}
public void MoveRight()
{
RB.velocity = new Vector2(speed, RB.velocity.y);
}
public void StopMoving()
{
RB.velocity = new Vector2(0f, RB.velocity.y);
}
void DetectInput()
{
float x = Input.GetAxisRaw("Horizontal");
if (x > 0)
{
MoveRight();
}
else if (x < 0)
{
MoveLeft();
}
else
{
StopMoving();
}
}
}
and:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Touch : MonoBehaviour
{
private Movement movement;
// Start is called before the first frame update
void Start()
{
movement = GetComponent<Movement>();
}
// Update is called once per frame
void Update()
{
if (Input.touchCount > 0)
{
UnityEngine.Touch touch = Input.GetTouch(0);
Vector3 touch_Pos = Camera.main.ScreenToWorldPoint(touch.position);
if (touch_Pos.x > 0 && touch_Pos.y < 0)
{
movement.MoveRight();
}
else if (touch_Pos.x < 0 && touch_Pos.y < 0)
{
movement.MoveLeft();
}
else
{
movement.StopMoving();
}
}
}
}
This is only one of many ways which I used to moving player. I also tried OnPointerDown, OnMouseDown and some other ways which I don't remember and got the same problem. I came to conclusion that the reason is different.
So, the scripts above worked perfect when I built the game for mobile. Player moved smooth as I want. But then I decided to add the fourth object on the scene - EventSystem! I didn't need to add it, but it was just an experiment. And I got the reason of my headache! EventSystem add the micro lag on the first touch on mobile. I have no idea how to fix this. Help me please!
Your answer
Follow this Question
Related Questions
Graphics Emulation - OpenGL 3 1 Answer
Unity 5 editor set play width and height in pixels 1 Answer
Object Painter Editor Help 1 Answer
How to make Textured Wire mode non-transparent 0 Answers
What is the problem with the 2020.1.8 android module? 0 Answers