Hi,
Can anyone help with my commenting for this script?
I have managed to comment all my other scripts explaining what the code is doing but I am having trouble with this one. It is the scrolling for my game, I followed tutorials for this section but unsure how to explain the code correctly.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ScrollParallex : MonoBehaviour
{
public float FOV = 19;
int leftIndex, rightIndex;
Transform cameraT;
Transform[] parallexLayers;
// Start is called before the first frame update
void Start()
{
cameraT = Camera.main.transform;
parallexLayers = new Transform[transform.childCount];
for (int index = 0; index < transform.childCount; index++)
{
parallexLayers[index] = transform.GetChild(index);
}
leftIndex = 0;
rightIndex = parallexLayers.Length - 1;
}
// Update is called once per frame
void Update()
{
if (cameraT.position.x < (parallexLayers[leftIndex].position.x + FOV))
{
moveBackgroundLeft();
}
if (cameraT.position.x > (parallexLayers[rightIndex].position.x + FOV))
{
moveBackgroundRight();
}
}
void moveBackgroundLeft()
{
parallexLayers[rightIndex].position = Vector3.right * (parallexLayers[leftIndex].position.x - FOV);
leftIndex = rightIndex;
rightIndex -= 1;
checkIndex();
}
void moveBackgroundRight()
{
parallexLayers[leftIndex].position = Vector3.right * (parallexLayers[rightIndex].position.x + FOV);
rightIndex = leftIndex;
leftIndex += 1;
checkIndex();
}
void checkIndex()
{
if (leftIndex == parallexLayers.Length)
{
leftIndex = 0;
}
if (rightIndex < 0)
{
rightIndex = parallexLayers.Length - 1;
}
}
}