- Home /
Android Lag ( Movement via GUI Buttons )
Hi, my name is Ivan.
I am creating a infinite runner game and you move left and right in the game by tapping the GUI Buttons. My problem is when you are moving it lags so much that you can't even play the game. Here is the code:
#pragma strict
var ballSpeed = 0.001;
var ballLeftLimit : float;
var ballRightLimit : float;
function OnGUI()
{
if(GUI.RepeatButton(Rect(0, Screen.height - 100, 100, 100), "<"))
{
if(transform.position.x > ballLeftLimit)
transform.position.x -= ballSpeed;
}
if(GUI.RepeatButton(Rect(Screen.width - 100, Screen.height - 100, 100, 100), ">"))
{
if(transform.position.x < ballRightLimit)
transform.position.x += ballSpeed;
}
}
And here is the picture of a profiler:
Is there any solution for this problem, if so please help me out :)
Best regards, Ivan!
You have several problems: Firstly, don't ever do movement in OnGUI (or, in fact, do anything other than GUI). OnGUI() gets called somewhat unpredictably, multiple times per frame and is not suitable for processing any game logic. $$anonymous$$ove it into Update(), and then also apply Time.deltaTime scaling factor to make it framerate independent.
This is pretty basic stuff - seeing as you've paid for a Unity Pro licence, I suggest you take the time to follow some tutorials.
Your answer

Follow this Question
Related Questions
Android touch input lag 0 Answers
I can't change the sprite using the Touch class 0 Answers
How do I prevent / deal with input lag on a touch interface? 3 Answers
Touch Input madness 2 Answers
GoogleVR vs GearVR - frame dropping 0 Answers