Search Unity

How to get 60 fps constant scrolling

Discussion in 'PSM' started by lunoka, Jul 15, 2014.

  1. lunoka

    lunoka

    Joined:
    Jul 3, 2012
    Posts:
    16
    Hello,

    I have some difficulties to produce a constant 60 fps scrolling under Unity and PS Vita. I made very simple script, you can find the project attached. There is no instantiation, no destroy behind ( so no garbage collection ), but the scrolling is not smooth.

    I've tried to modify the VSync params for the build but no success. Using FixedUpdate doesn't do the trick either.

    I'm working actually on a 2D horizontal shooter ( https://twitter.com/Munesoft ) and It's a pity if I couldn't get a full 60 fps rate.

    Any idea ? or just the Unity engine can't provide a constant rate ?

    Script code :

    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;

    public class ScrollingBG : MonoBehaviour {

    public Vector3 speed;
    public float loopLimit;
    private Vector3 scrolled;
    private Vector3 startPosition;
    private GameObject go;
    private bool updated;

    // Use this for initialization
    void Start () {
    go = gameObject;
    startPosition = go.transform.position;
    }

    // Update is called once per frame
    void Update () {
    Vector3 delta = speed * Time.deltaTime * 60f;
    delta = new Vector3((int)delta.x,(int)delta.y,(int)delta.z);
    scrolled += delta;
    go.transform.Translate(delta);

    if(scrolled.x <= loopLimit)
    {
    go.transform.position = startPosition;
    scrolled = Vector3.zero;
    }
    }

    void OnGUI()
    {
    GUI.color = Color.yellow;
    GUI.Label(new Rect(0,300,200,20), "DeltaTime " + Time.deltaTime);
    }
    }
     

    Attached Files:

    • Test.zip
      File size:
      105.8 KB
      Views:
      467
  2. lunoka

    lunoka

    Joined:
    Jul 3, 2012
    Posts:
    16
  3. IanStanbridge

    IanStanbridge

    Joined:
    Aug 26, 2013
    Posts:
    334
    Have you tried using the wait command ? I'd suggest making your update loop wait for each fixed time step so if it did enter the update loop again too early it would just wait. Or if the fixed time step isn't doing the trick add a timer to the update loop and make it wait for the difference between the previous update time and a 60th of a second.
     
  4. lunoka

    lunoka

    Joined:
    Jul 3, 2012
    Posts:
    16
    Thanks for reply,

    I test each update if Time.deltaTime is under 0.0166667, if so, I clamp the value. Unfortunately, the result isn't really good. It seems like it is impossible on modern mobile devices to obtain smooth scrollings with no hickups like 16/32 bits consoles were able to provide ( hardware was built to do so ).
     
  5. lunoka

    lunoka

    Joined:
    Jul 3, 2012
    Posts:
    16
    Well, after more investigations, it came up that the main culprit was OnGUI() function -_-; .After removing this from my scripts, the scrolling is smooth. I did know this function reduced performance but not to that point...
     
  6. dragula96

    dragula96

    Joined:
    Dec 6, 2013
    Posts:
    1
    I was having the same problem all day going crazy on why it was doing that, thanks , removing the OnGUI() function did infact take care of the 1 second hickup.