Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Linear movement lag/stuttering, even with just one cube. Please help.

Discussion in 'Editor & General Support' started by Miomek, Nov 16, 2017.

  1. Miomek

    Miomek

    Joined:
    Mar 26, 2016
    Posts:
    4
    Hi, I made Android game in Unity but when testing I noticed some serious lag spikes. I decided to isolate the issue and the lag is present in a simple scene with just one moving cube. So what happens is, I run the app on my device(G Flex 2) and it runs fine at 58fps and then suddenly the fps drops and the cube starts lagging a little.

    It's not a big problem for a cube but it also affects the camera. In my game, camera is following the player and the stuttering is very visible there.

    I uploaded example project here: https://drive.google.com/file/d/1Sg9sC9QH5sp08MIsSzy85jFWh646FOAM/view?usp=sharing

    "MAIN" scene has cube in it and "SCENE2" has the camera movement. To reset the position of the cube/camera just tap the screen. Sometimes it takes a minute before the lag happens.

    It's important to test it on Android, in editor it's fine.
     
  2. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,294
    I haven't downloaded project (sorry), but that seems like camera movement issue rather than fps one.

    Make sure you're moving your objects in FixedUpdate() and camera is moved in LateUpdate().
    Also, it would be rather more usefull to see the actual code of it without downloading anything.

    Use tags for that next time.
    Code (csharp):
    1.  Hello tags
     
  3. Miomek

    Miomek

    Joined:
    Mar 26, 2016
    Posts:
    4
    I was using Update() for the movement so I tried changing it to FixedUpdate() and LateUpdtate() but it didn't help.

    Here is code for the camera:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Move : MonoBehaviour {
    6.  
    7.     Vector3 initialPosiiton;
    8.     public int verticalSpeed;
    9.    
    10.     void Start () {
    11.         initialPosiiton = transform.position;
    12.     }
    13.  
    14.     void LateUpdate () {
    15.         transform.position += new Vector3(0, verticalSpeed * Time.deltaTime, 0);
    16.  
    17.         if (Input.GetMouseButtonDown(0) || Input.touchCount > 0)
    18.             transform.position = initialPosiiton;
    19.     }
    20. }
    I also use this script to display fps:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class FPSmonitor : MonoBehaviour {
    6.  
    7.     float deltaTime = 0.0f;
    8.  
    9.     void Awake()
    10.     {
    11.         Application.targetFrameRate = 60;
    12.     }
    13.  
    14.     void Update()
    15.     {
    16.         deltaTime += (Time.deltaTime - deltaTime) * 0.1f;
    17.     }
    18.  
    19.     void OnGUI()
    20.     {
    21.         int w = Screen.width, h = Screen.height;
    22.  
    23.         GUIStyle style = new GUIStyle();
    24.  
    25.         Rect rect = new Rect(0, 0, w, h * 2 / 100);
    26.         style.alignment = TextAnchor.UpperLeft;
    27.         style.fontSize = h * 2 / 100;
    28.         style.normal.textColor = new Color(0.0f, 0.0f, 0.5f, 1.0f);
    29.         float msec = deltaTime * 1000.0f;
    30.         float fps = 1.0f / deltaTime;
    31.  
    32.         string text = string.Format("{0:0.0} ms ({1:0.} fps)", msec, fps);
    33.         //Debug.Log(text);
    34.         GUI.Label(rect, text, style);
    35.     }
    36. }
     
  4. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,294
    If you're using this kind of setup:
    E.g. Cube is moving (In FixedUpdate) and Camera is located at separately and moved in LateUpdate it should be fine. Although for follow you should use something like Lerp, Slerp or MoveTowards.

    In current state - you're moving camera with *Time.deltaTime multiplication without any smoothing done at all.
    That's why it's stuttering. Try applying something of the above math methods to the camera in LateUpdate.

    Also, reason for lag spikes could be the garbage allocation generated by your fps script.
    Not to mention that OnGUI is slow on itself
     
  5. kobyfr

    kobyfr

    Joined:
    Aug 21, 2020
    Posts:
    7
    increasing the position iterations in the project settings, might help in this case.
    upload_2020-11-18_21-3-50.png
    it improved the overall movement smoothness, and mostly my objects moves w/o lags.
    However, every random amount of time, object movement still experience lags.