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!