2
\$\begingroup\$

I'm trying to build a 2D game for android and after I finished developing the game I noticed that there was lag present when moving objects around. I tested it on a phone much more powerful than my own Sony Xperia z2 and it had more lag, therefore I believe it's not related to RAM.

I've disabled vsync using profiler putting the movement in fixedupdate, lateupdate, and even built a new project.
This new project has only 1 scene and 3 scripts. One of scripts are attached to this sprite to move it. When running this project I still have lag; both on my laptop and on my mobile phone!

This is the script:

using UnityEngine;
using System.Collections;

public class PlayerMove : MonoBehaviour {
    public Vector2 movement;
    public Vector2 speed;
    private float myTimer;
// Use this for initialization
void Start () {
    speed=new Vector2(2,0);
}

// Update is called once per frame
void Update () {
    handleMove ();


}
public void handleMove(){
    if (Input.GetKey (KeyCode.UpArrow)) {
        speed.y=2;
        this.gameObject.rigidbody2D.velocity=speed;
        myTimer=.3f;
    }
    if (Input.GetKey (KeyCode.DownArrow)) {
        speed.y=-2;
        this.gameObject.rigidbody2D.velocity=speed;
        myTimer=.3f;
    }
    if (myTimer > 0)
        myTimer -= Time.deltaTime;
    else {
        speed.y=0;
        this.gameObject.rigidbody2D.velocity=speed;
    }
}
}

this is results of profiler profiler results

\$\endgroup\$
  • 1
    \$\begingroup\$ What exactly do you mean with "lag"? Low framerate? Irregular framerate? Irregular movement speed of sprites? Delayed reaction to input? People misuse that word so often that its actual meaning is quite fuzzy by now. \$\endgroup\$ – Philipp Nov 26 '15 at 15:29
  • \$\begingroup\$ by lag i mean something like hiccup. it moves fine but it's like it freezes (for a very short time) and then jumps ahead! \$\endgroup\$ – HotShot Nov 26 '15 at 15:41
  • 1
    \$\begingroup\$ Possible duplicate of Update vs. FixedUpdate in Unity - Input handling should be in Update() while the actual game mechanics which act on the current input should be in FixedUpdate(). \$\endgroup\$ – Philipp Nov 26 '15 at 16:12
  • \$\begingroup\$ this is the only script running. and i tried using fixedupdate instead of update but the result was the same with Update. \$\endgroup\$ – HotShot Nov 26 '15 at 16:25
  • \$\begingroup\$ also i am not using any input. i'm just watching the sprit move! \$\endgroup\$ – HotShot Nov 26 '15 at 16:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.