Parallaxing Tutorial. Understanding the logic.

Ok so before I even attempted to ask this question I thought about this little piece of code for about a week. I wasn’t even sure what to ask. That’s how cryptic it is to me.

parallaxScales = new float[backgrounds.Length];
		for (int i = 0; i < backgrounds.Length; i++) {     
			parallaxScales <em>= backgrounds_.position.z*-1; and apply z position * -1_</em>

* }*
Anyways I guess I was just wondering if someone could explain what is going on because i’ve never really seen anything like it before. It seems like a new a new float, an array, is being assigned to this variable. but then it shows this same variable(a float) having a position assigned to it. (I hope that made sense, still a beginner) I was wondering if someone could break it down a little more than the person who made the tutorial did.
Here is the entire script and link for the tutorial 3. How to make a 2D Platformer - Parallax Scrolling - Unity Tutorial - YouTube
using UnityEngine;
using System.Collections;

public class Parallaxing : MonoBehaviour {

* public Transform[] backgrounds; *
* private float[] parallaxScales; *
* public float smoothing = 1f; *

* private Transform cam; *
* private Vector3 previousCamPos; *

* void Awake () {*

* cam = Camera.main.transform;*
* }*

* void Start () {*

* previousCamPos = cam.position;*

* parallaxScales = new float[backgrounds.Length];*
* for (int i = 0; i < backgrounds.Length; i++) {*
parallaxScales = backgrounds_.position.z*-1;
* }*_

* }*

* void Update () {*

* for (int i = 0; i < backgrounds.Length; i++) {*

_ float parallax = (previousCamPos.x - cam.position.x) * parallaxScales*;*_

_ float backgroundTargetPosX = backgrounds*.position.x + parallax;*_

Vector3 backgroundTargetPos = new Vector3 (backgroundTargetPosX, backgrounds_.position.y, backgrounds*.position.z);*_

backgrounds.position = Vector3.Lerp (backgrounds_.position, backgroundTargetPos, smoothing * Time.deltaTime);
* }*_

* previousCamPos = cam.position;*
* }*
}

This is a month old, you may or may not have figured this out yet. Answering anyway for people that might come across this randomly.

The lines you asked about do just a few simple things.

         parallaxScales = new float[backgrounds.Length];
         for (int i = 0; i < backgrounds.Length; i++) {      
             parallaxScales <em>= backgrounds_.position.z*-1;_</em> 

}

The first line sets the size of the array called parallaxScales to be equal to the size of the array called backgrounds. “.Length” returns an integer that represents the number of elements in an array.
“backgrounds” is an array containing references to the transforms of the gameobjects which will be parallaxed.
the next 2-3 lines are a for loop which iterates through each element of the array “backgrounds” and sets each float in the relative element of the array “parallaxScales” to be equal to the z position of the objects referenced in backgrounds.
So you attach gameobjects to the script in the inspector which sets them in the “backgrounds” array. The first line checks how many gameobject’s transforms you attached and makes a new element in the parallaxScales array for each one.
-
A for loop is essentially an if statement that runs over and over until the condition set in it are no longer true. If they are always true, your game will crash.
for ( variable initialization; condition; variable update ) {
Code to execute while the condition is true
}
-
In this case, the variable is an integer “i” which is equal to 0.
the condition is " i < backgrounds.Length "
the variable update adds 1 to i.
when the for loop is first ran i = 0 obviously, and i is less than backgrounds.Length, so the code below executes:
parallaxScales = backgrounds_.position.z*-1;
i is equal to 0 right now, so element 0 of parallaxScales[0] is being set to equal backgrounds[0].position.z*-1.
Once that happens, 1 is added to i so it now equals 1.
it repeats because i < backgrounds.Length
Now i is equal to 1 so the element parallaxScales[1] is being set to equal backgrounds[1].position.z*-1.
This continues until i is equal to or greater than the number of elements in “backgrounds”.
-
I really hope that made sense._