Search Unity

Parallax effect has gaps in it

Discussion in '2D' started by Hurricane59, Sep 7, 2019.

  1. Hurricane59

    Hurricane59

    Joined:
    Mar 23, 2019
    Posts:
    4
    I am making a game where the background moves past the player, and then gets deleted and instantiated outside of the camera to create a parallax effect. The big problem is every so often I'll get gaps in between the background and I don't know how to fix it.

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class Spawner : MonoBehaviour
    7. {
    8.  
    9.     public GameObject[] stages;
    10.  
    11.     public GameObject test;
    12.  
    13.  
    14.     public BoxCollider2D myCollider;
    15.     public Spikes mySpikes;
    16.  
    17.     public static Spawner instance;
    18.  
    19.     public void Awake()
    20.     {
    21.         instance = this;
    22.     }
    23.  
    24.     // Start is called before the first frame update
    25.  
    26.     private void Update()
    27.     {
    28.        
    29.     }
    30.     // Update is called once per frame
    31.     public void create()
    32.     {
    33.  
    34.         int rand = Random.Range(0, stages.Length);
    35.        
    36.         test = Instantiate(stages[rand], transform.position, Quaternion.identity);
    37.      
    38.        
    39.  
    40.  
    41.     }
    42.    
    43. }
    44.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Spikes : MonoBehaviour
    6. {
    7.  
    8.     public float speed;
    9.  
    10.     // Start is called before the first frame update
    11.     void Start()
    12.     {
    13.        
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void Update()
    18.     {
    19.         transform.Translate(Vector2.left * speed * Time.deltaTime);
    20.         if(transform.position.x <= -14)
    21.         {
    22.             Destroy(gameObject);
    23.             Spawner.instance.create();
    24.         }
    25.     }
    26.    
    27. }
     
  2. Onny98

    Onny98

    Joined:
    Sep 7, 2019
    Posts:
    1
    The way you are doing parallax is super inefficient since you are instantiating all the time. I recommend you to change the sprite position based in the camera movement. I will leave you the script to achieve this.

    Code (CSharp):
    1. using System.Collections.Generic;
    2. using UnityEngine;
    3.  
    4. [ExecuteInEditMode]
    5. public class ParallaxingScript : MonoBehaviour
    6. {
    7.     public static ParallaxingScript instance;
    8.  
    9.     [Header("Backgrounds that move in x and y")]
    10.     public List<Transform> backgrounds;
    11.     [Header("Background that only move in x")]
    12.     public List<Transform> backgroundsX;
    13.     [SerializeField] private float smoothing = 1f;
    14.  
    15.     private float[] parallaxScales;
    16.     private float[] parallaxScalesX;
    17.     private Transform cam;
    18.     private Vector3 previousCamPos;
    19.  
    20.     private void Awake()
    21.     {
    22.         instance = this;
    23.         if (Camera.main != null) cam = Camera.main.transform;
    24.     }
    25.  
    26.     private void Start() {
    27.         previousCamPos = cam.position;
    28.     }
    29.  
    30.     private void Update() {
    31.         parallaxScales = new float[backgrounds.Count];
    32.         for (var i = 0; i < backgrounds.Count; i++) {
    33.             parallaxScales[i] = backgrounds[i].position.z * -1;
    34.         }
    35.         parallaxScalesX = new float[backgroundsX.Count];
    36.         for (var i = 0; i < backgroundsX.Count; i++) {
    37.             parallaxScalesX[i] = backgroundsX[i].position.z * -1;
    38.         }
    39.         for (var i = 0; i < backgrounds.Count; i++) {
    40.             var position = cam.position;
    41.             var parallaxX = (previousCamPos.x - position.x) * parallaxScales[i];
    42.             var parallaxY = (previousCamPos.y - position.y) * parallaxScales[i];
    43.  
    44.             var backgroundTargetPosX = backgrounds[i].position.x + parallaxX;
    45.             var backgroundTargetPosY = backgrounds[i].position.y + parallaxY;
    46.  
    47.             var backgroundTargetPos = new Vector3(backgroundTargetPosX, backgroundTargetPosY, backgrounds[i].position.z);
    48.             backgrounds[i].position = Vector3.Lerp(backgrounds[i].position, backgroundTargetPos, smoothing * Time.fixedDeltaTime);
    49.         }
    50.  
    51.         for (var i = 0; i < backgroundsX.Count; i++) {
    52.             var parallaxX = (previousCamPos.x - cam.position.x) * parallaxScalesX[i];
    53.  
    54.             var backgroundTargetPosX = backgroundsX[i].position.x + parallaxX;
    55.  
    56.             var backgroundTargetPos = new Vector3(backgroundTargetPosX, backgroundsX[i].position.y, backgroundsX[i].position.z);
    57.             backgroundsX[i].position = Vector3.Lerp(backgroundsX[i].position, backgroundTargetPos, smoothing * Time.fixedDeltaTime);
    58.         }
    59.         previousCamPos = cam.position;
    60.     }
    61. }