- Home /
Input lag on Andorid but not in Unity
I made a simple script to move an object on the X and Y axis. It works fine in Unity, but when I build it and run it on android I get lag, meaning that the object changes direction after a delay and not right when I press the button. The code I have:
void FixedUpdate () {
if (!isMoving)
return;
//Controls();
Vector2 temp = new Vector2(transform.position.x + direction.x * speed * Time.deltaTime * 50,
transform.position.y + direction.y * speed * Time.deltaTime * 50);
gameObject.transform.position = temp;
}
This code is called when the left or right button is pressed and it simply changes the direction vector:
public void Controls()
{
if (Input.GetKeyDown("left") || leftPressed)
{
if (direction == new Vector2(0, 1))
{
direction = new Vector2(-1, 0);
}
else if (direction == new Vector2(1, 0))
{
direction = new Vector2(0, 1);
}
else if (direction == new Vector2(0, -1))
{
direction = new Vector2(1, 0);
}
else if (direction == new Vector2(-1, 0))
{
direction = new Vector2(0, -1);
}
}
else if (Input.GetKeyDown("right") || rightPressed)
{
if (direction == new Vector2(0, 1))
{
direction = new Vector2(1, 0);
}
else if (direction == new Vector2(1, 0))
{
direction = new Vector2(0, -1);
}
else if (direction == new Vector2(0, -1))
{
direction = new Vector2(-1, 0);
}
else if (direction == new Vector2(-1, 0))
{
direction = new Vector2(0, 1);
}
}
leftPressed = rightPressed = false;
}
This could happen if your Android test device is not as performant as the PC where your run Unity. Does it behave like this on many Android devices that you test, or just one? Other than that, if there's something I would do to optimize the code, it would be using the predefined values of Vector2, ins$$anonymous$$d of all the new Vector2(x, y) in the code. i.e. use Vector2.down
ins$$anonymous$$d of new Vector2(0, -1)
, or Vector2.right
ins$$anonymous$$d of Vector2(1, 0)
, etc. See:
https://docs.unity3d.com/ScriptReference/Vector2.html
So, replace all these in your code and try again and see if it makes any difference.
Thank you for your reply! I was testing it on my own phone only, a 8 core Huawei P9 Lite. I don't think that the new Vector2() is the issue, since it is called only whenever the left or right keys are pressed. Do you think the lag could be because of creating a new Vector2 in the FixedUpdate function?
@11tomi12 you say
object changes direction after a delay and not right when I press the button
Don't I understand well what you are saying? I understand that you press a button, so Controls()
is called, then there's a delay, then direction changes. I'm a bit confused why you have commented out the call to Controls()
in line 4 inside FixedUpdate ()
. I assumed that Controls()
does get called in line 4 under normal conditions. Otherwise, if Controls()
is never called, why do you post its code? So, why you now say
it is called only whenever the left or right keys are pressed
From the code you have posted, I can only see direction changing only after the left or right keys are pressed. What am I missing?
You should not be having any performance problems with Huawei P9 Lite. I would be surprised if new Vector2
in the FixedUpdate function could create such a lag. However, I would personally declare Vector2 temp
as a class variable anyway, since it is used many times per frame.
I commented out Controls()
, because I used Input.Get$$anonymous$$ey
in it for testing purposes(left and right keyboard keys), but now that I use buttons for it it doesn't have to be called every frame. You are right, Controls()
gets called whenever left or right button is pressed. The button pressed sets it's corresponding boolean to true, after that Controls()
gets called and changes the direction Vector()
. Could it be possible that Unity has some weird default settings for android, which would cause the delay?
Answer by 11tomi12 · Nov 11, 2017 at 11:38 PM
Found the problem!
Since I'm using OnClick()
for the buttons, the function runs AFTER the button is release, meaning obviously delay. I fixed this by simply adding an Event Trigger
component to the buttons and added a PointerDown
event, which runs the code right after the button is pressed.
Your answer
Follow this Question
Related Questions
how to rolle a ball by touch or mouse 0 Answers
'GetAxis' is not a member of 'function(): void' 1 Answer
How do I make my player jump on click on the button? 3 Answers
Button not responsive 1 Answer