Search Unity

Background Scrolling Laggy / Stuttery

Discussion in 'Editor & General Support' started by StickBit, Jun 2, 2020.

  1. StickBit

    StickBit

    Joined:
    Nov 27, 2016
    Posts:
    2
    Hi, I am developing a Mario clone in Unity 2D and I am working on getting the background to scroll. Currently I am using this code to move the background with the camera, relative to the spawn point:

    Code (CSharp):
    1. void Update() {
    2.         if (player != null) {
    3.             Vector3 position = t.position;
    4.             double distX = position.x - GameWorld.spawnPoint.x;
    5.             double distY = position.y - GameWorld.spawnPoint.y;
    6.             transform.position = new Vector2((float) (distX / 2), (float) distY / 2f-1f);
    7.         }
    8.     }
    This results in the background to move around at a weird speed when the camera is controlled by the player. However, when the player dies, the camera transitions back to the spawn point, where the background moves much more smoothly.


    The issue is a lot more noticeable on the game itself, rather than the video. (You may also need to set the playback to 60 FPS) Any help would be appreciated. Thanks!
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,914
    What is the "t" transform? How is it moving? It looks like the movement of your background is based on whatever that movement is. Also does GameWorld.spawnPoint move around?
     
  3. StickBit

    StickBit

    Joined:
    Nov 27, 2016
    Posts:
    2
    t is the Transform of the camera. The spawn point does not move.
     
  4. PixellPat

    PixellPat

    Joined:
    Jun 13, 2021
    Posts:
    9
    I'm having the same problem. I have a script on the camera that follows the player. When the player moves fast the background gets blurry and stuttery. Any fix?