- Home /
 
Parallax scrolling background performance problem
So i have a parallaxing background in a 2d game im making. The problem is when i port it to my android phone and run it, it runs fine for the first 20 seconds or so, after that it starts to run laggy/sluggish, seems like a framerate issue but im not to sure because all other elements in the game runs perfect however> Appreciate any help. Here is the code i use for the parallax background.
 using UnityEngine;
 using System.Collections;
 
 public class Backgrounds : MonoBehaviour {
     public float speed = 0;
 
 
     // Use this for initialization
     void Start () {
 
     
     }
     
     // Update is called once per frame
     void Update () {
         renderer.material.mainTextureOffset = new Vector2 (Time.time * speed, 0f);
     
     }
 }
 
 
              Another things is to the more times i replay the scene (on phone) with the parallax background the laggier and choppier it gets.
Answer by kyly1992 · Jun 19, 2014 at 04:15 PM
So i figured it out had to take out Time.time from the code. Apparently that was taking up to much resources
Taking a second look at things, you probably want Time.deltaTime ins$$anonymous$$d of Time.time anyway
I have the same problem, this solution didnt work for me.
Answer by TiagoMachado · Oct 09, 2014 at 12:01 PM
use this...
using UnityEngine; using System.Collections;
public class MoveOffSet : MonoBehaviour {
 private Material currentMaterial;
 public float scrollSpeed;
 private float offset;
 void Start(){
     
     currentMaterial = renderer.material;
     
 }
 
 
 void FixedUpdate() {
     offset += scrollSpeed * Time.deltaTime;
     offset = offset % 1.0f;
     currentMaterial.mainTextureOffset = new Vector2(offset,0); 
 
 }
 
 
 
 
               }
Answer by thomasindustry · Jun 16, 2014 at 09:31 PM
Try saving the material in a variable at the start instead of accessing it each frame. It may increase performance.
 using UnityEngine;
 using System.Collections;
 
 public class Backgrounds : MonoBehaviour
 {
     public float speed = 0;
 
     Material theMaterial;
 
     // Use this for initialization
     void Start()
     {
         theMaterial = renderer.material;
 
     }
 
     // Update is called once per frame
     void Update()
     {
         theMaterial.mainTextureOffset = new Vector2(Time.time * speed, 0f);
 
     }
 }
 
              ok i played the game for countless turns and the lag is still there
runs very fine in the beginning but when i die and click retry a number of times the parallax background lags a lot, very noticeable
I have the same problem, this solution didnt work for me.
Answer by Akrog · Oct 21, 2014 at 11:43 AM
  theMaterial.mainTextureOffset = new Vector2((Time.time * speed)%1, 0f);
 
 
               Enjoy!
Your answer
 
             Follow this Question
Related Questions
Moving Background with translated images 1 Answer
Problem with changing the direction of an Infinite scrolling background. 1 Answer
2D Parallax Background 4 Answers
trouble with getting the background to show past the midground in 2d parallax backgrounds 0 Answers
How to create an infinite scrolling background in top down multi-directional shooters. 2 Answers