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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to fix UI jitter when changing camera ortographic size?

Discussion in 'UGUI & TextMesh Pro' started by laserbeam, May 26, 2015.

  1. laserbeam

    laserbeam

    Joined:
    Aug 4, 2014
    Posts:
    9
    Hey guys, I have a 2D game and an orthographic camera pointed at the 2D world. I want to be able to zoom in and out to see more of the 2D world, and normally, I could do this using ortographicSize, no problem. But if I do that, the UI lags behind the camera and looks really really bad.

    So, if I have a Canvas set in Camera Render mode, and I change the ortographicSize of that camera, the UI moves, I don't want it to move :(. I've been stuck on this for hours :(.

    I've included a simple example where if you drag that slider, the orthographicSize of the camera is changed and the UI jitters.

    Also note I'm using Unity 4.6.x, and switching to 5.0 is not an option at this point due to some 3rd party dependencies.

    PS. If I change ortographicSize in the editor, everything is fine. If I change it through user input, all hell breaks loose. Also, the UI was implemented quite a while back, and switching to a perspective camera and moving it could cause a lot of problems.
     

    Attached Files:

  2. laserbeam

    laserbeam

    Joined:
    Aug 4, 2014
    Posts:
    9
    There's several reasons why I don't want to switch to render mode overlay, and they mostly deal with a reasonably large codebase and converting coordinates from screen space to world space. I am looking for solutions to using Screen Space - Camera. If that's not an option yeah, some form of switching might be done, but will be painful. Either Screen Space - Overlay, Using a 2nd camera or making the camera to perspective. These are all expensive changes as they affect assumptions made in the rest of the codebase.
     
  3. laserbeam

    laserbeam

    Joined:
    Aug 4, 2014
    Posts:
    9
    Found one solution. Apparently if I wait for the end of the frame, all works. I don't like this 100%, but that's the best solution I have right now.

    Code (CSharp):
    1.     private IEnumerator _changeCameraSize(float value)
    2.     {
    3.         yield return new WaitForEndOfFrame();
    4.         DragCamera.orthographicSize = value;
    5.     }
    6.  
     
  4. Mistale

    Mistale

    Joined:
    Apr 18, 2012
    Posts:
    173
    One quick suggestion; if you dont want the ui to be affected by the orthographic size of your main camera, put the ui on a separate camera.
     
    rakkarage likes this.
  5. laserbeam

    laserbeam

    Joined:
    Aug 4, 2014
    Posts:
    9
    We thought of that Mistale, but that is a bit too costly of a change right now in the code base. This was one of those things that you realised half way through the project that could happen, ui lagging behind the scaling o_O.

    We managed to get all features we needed working with that coroutine though. It's really hackish, like we change the scale for computation purposes (transforming between coordinate systems during scaling and doing multitouch gestures for scaling), change it back so UI doesn't wiggle, and then actually make the change next frame. VERY convoluted, but it's the only thing we managed to do to get this to work.

    Hope this helps someone. And yeah, for new projects, we'll set up multiple cameras for this from the start.
     
    Mistale likes this.
  6. rakkarage

    rakkarage

    Joined:
    Feb 3, 2014
    Posts:
    683
    Mistale likes this.
  7. laserbeam

    laserbeam

    Joined:
    Aug 4, 2014
    Posts:
    9
    Yes, of course, the problem with changing though is in the code, not in the scene (so we do a lot of adding game objects to the scene from the UI, and a lot of coordinate conversions between different canvases, also we overwrite a lot of how input goes, a lot of game objects assume Camera.main is a thing... etc.). Moving things to different cameras is a risk because of that.
     
  8. killroy

    killroy

    Joined:
    Nov 8, 2011
    Posts:
    15
    Wow, thank you! I had the same problem and your solution fixes it. Saved me hours of redoing layers which would probably cause more bugs.
     
  9. laserbeam

    laserbeam

    Joined:
    Aug 4, 2014
    Posts:
    9
    Welcome, but I still feel it's a huge hack...