Issues with framerate/jerkiness when using 2d scrolling background.

Hello,
I am currently making a 2d, side scrolling endless runner, and am having some issues with framerate/ jerkiness when using scrolling backgrounds.

Currently the game runs beautifully when the scrolling backgrounds are disabled, and functionally the game plays well with the backgrounds enabled, but has a very noticable stutter/jerkiness.

In the game the player moves, using the following bit of relevant code:

public float moveSpeed;
private Rigidbody2D rb;

void Start()
{
     rb = GetComponent<Rigidbody2D>();
}

void FixedUpdate()
{
      rb.velocity = new Vector2 (moveSpeed , rb.velocity.y);
}

The camera follows the player using the following script:

using UnityEngine;
using System.Collections;

public class CameraController : MonoBehaviour
{
    public PlayerController thePlayer;
    private Vector3 lastPlayerPosition;
    private float distanceToMove;
   
    void Start ()
    {
        thePlayer = FindObjectOfType<PlayerController>();
        lastPlayerPosition = thePlayer.transform.position;
    }
   

    void LateUpdate ()
    {
        distanceToMove = thePlayer.transform.position.x - lastPlayerPosition.x;

        transform.position = new Vector3(transform.position.x + distanceToMove, transform.position.y, transform.position.z);

        lastPlayerPosition = thePlayer.transform.position;
    }
}

The player movement and the camera movement seem very smooth set up this way, and the platforms that you jump between as the camera scrolls move nice and smooth as well.

It is when I add in the scrolling backgrounds that I start seeing performance issues across standalone, android, and iOS.

The Scrolling backgrounds are quads with textures attached, set to repeat, and are all children of the camera.
The three scrolling background layers each have a script like the following:

using UnityEngine;
using System.Collections;

public class BackgroundScroll : MonoBehaviour
{

    public float speed;
    public bool isScrolling;
    public Renderer theMat;
    public float theOffset;
   
    void Start ()
    {
        isScrolling = true;
        theMat = GetComponent<Renderer>();
    }
   
    void Update ()
    {
        if (isScrolling)
        {
            theOffset = (theOffset + speed * Time.deltaTime) % 1;
            if (theOffset > 1.0f)
            {
                theOffset -= 1.0f;
            }
           
            theMat.material.mainTextureOffset = new Vector2 (theOffset, 0.0f);
           
           
        }
        if (!isScrolling)
        {
            return;
        }
    }
   
    public void ToggleScrolling()
    {               
        if(isScrolling)
        {
            isScrolling = false;
        }
        else if(!isScrolling)
        {
            isScrolling = true;
        }
    }
}

Does anyone see something I am missing? I have tried limiting the size of the textures on the quads, but that does not help. I have played with vsync, doesn’t help. I have added

void Awake ()
    {
            Application.targetFrameRate = 60;
    }

but this doesn’t seem to make a difference. Any help very appreciated I have been stuck on this for weeks!

I have also tried simply disabling the scripts on the background textures, but even with the scripts disabled, the background textures/quads make the game look choppy. There has to be something simple I am missing?

The background images in question:

It looks like the shader issue… Can you tell me what’s the setting of your texture and the settings of your material… For material settings Im interested what kind of shader does it use… I would suggest you to use the mobile/particles/alpha blended and see if you have some improvements.

All of the sprites are using the default sprite material, and the textures on the quads in the background are using the mobile/particles/alpha blended shader.

I have a thread about this issue in the 2D section, which has more information. I am certain the issue is overdraw, from transparency. How to fix it, though, I don’t know.

I’ve been reading a tut before, which has the same scrolling backrounds of multiple quads… but never had any issue. though the code was a little bit different.

public Vector2 speed = Vector2.zero;
public Vector2 offset = Vector2.zero;

private Material material;
void Start()
{
material = GetComponent<Renderer>().material;
offset= material.GetTextureOffset("_MainTex");
}

void Update()
{
offset += speed* Time.deltaTime;
material.SetTextureOffset("_MainTex",offset);
}

As you can see in this code, it does get the texture offset at the start method, and then sets the TextureOffset in the Update method… I dont know maybe it will make some difference, you can try to rewrite your code which will use GetTextureOffset/SetTextureOffset… as I said I didnt have any problems. with multiple backgrounds scrolling at the same time… but that was the code I used.
This script have been attached to the quad itself but not on the mainCamera as you do.

What kind of tablet are you running this on? I also have my scroll script on the quad; the quads are children of the camera. I have tried several different scroll scripts, but like I said, even with the scroll script completely disabled the performance is bad; the issue is coming from the transparent images, not from the scroll script.

I run that code on Sony Xperia Z1 Compact.

Try to format your images into different one. Do they use True Color compression? try the compressed one, for the quads images… It also can be the size of your images… what is their size? Maybe they are too heavy for mobile and you should resize them.

Otherwise, I have no more clue). Strange thing it works quite well for me, though my images doesnt have any transparency.

Nothing strange about that. The fact that your images don’t have transparency is exactly why it works fine for you.

Also you are running this on a small screen. As I mentioned before, the game runs fine on desktop and on small phone sized screens (iphone5, samsung s5). Where I am having issues is on tablets where a larger screen is being driven by a small mobile gpu, which quickly increases the effects of overdraw/high fill rate.

iPhone 5 = runs great.
Samsung S5 = great.
Galaxy tab4 7" = horrid.
IPad3 (iPad w/retina) = shoot me please.