Advertisement
johnnygoodguy2000

Parallaxing.cs

Apr 23rd, 2024 (edited)
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.10 KB | Gaming | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Paralaxing : MonoBehaviour
  6. {
  7.     public Transform[] backgrounds;
  8.  
  9.     private float[] parallaxScales;
  10.  
  11.     public float smoothing;
  12.  
  13.     private Vector3 previousCameraPosition;
  14.     // Start is called before the first frame update
  15.     void Start()
  16.     {
  17.        
  18.         previousCameraPosition = transform.position;
  19.  
  20.         parallaxScales = new float[backgrounds.Length];
  21.  
  22.         for (int i = 0; i < parallaxScales.Length; i++)
  23.         {
  24.             parallaxScales[i] = backgrounds[i].position.z * -1;
  25.         }
  26.     }
  27.  
  28.     // Update is called once per frame
  29.     void LateUpdate()
  30.     {
  31.         for (int i = 0; i < backgrounds.Length; i++)
  32.         {
  33.             Vector3 parallax = (previousCameraPosition - transform.position) * ( parallaxScales[i] / smoothing);
  34.  
  35.             backgrounds[i].position = new Vector3(backgrounds[i].position.x + parallax.x, backgrounds[i].position.y + parallax.y, backgrounds[i].position.z);
  36.         }
  37.  
  38.         previousCameraPosition = transform.position;
  39.     }
  40. }
  41.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement