Search Unity

Parallax optimisation

Discussion in '2D' started by Cobra_2o1o, Sep 12, 2019.

  1. Cobra_2o1o

    Cobra_2o1o

    Joined:
    Jul 25, 2016
    Posts:
    39
    Hello there,

    My game really struggles when I turn on the background. With it turned off, the game is capped to 60fps and with the background on it drops to 44fps.

    I use 5 layers of background, each layer has 3 images, so in total, 15 images (12 of them with transparency) that use the following script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Parallax : MonoBehaviour
    6. {
    7.     private float lenght, startpos;
    8.     public GameObject cam;
    9.     public float parallaxEffect;
    10.  
    11.     void Start()
    12.     {
    13.         startpos = transform.position.x;
    14.         lenght = GetComponent<SpriteRenderer>().bounds.size.x;
    15.     }
    16.  
    17.  
    18.     void Update()
    19.     {
    20.         float temp = cam.transform.position.x * (1 - parallaxEffect);
    21.         float dist = cam.transform.position.x * parallaxEffect;
    22.  
    23.         transform.position = new Vector3(startpos + dist, transform.position.y, transform.position.z);
    24.  
    25.         if (temp > startpos + lenght) startpos += lenght;
    26.         else if (temp < startpos - lenght) startpos -= lenght;
    27.     }
    28. }
    29.  

    These are my settings and sprites:
    Untitled.png