Hello everyone,
I am trying to figure out what I am missing from my code. What I am trying to do is have the ability to repeat a background of 3 images side by side to create a parallax background. I want it to follow my player. I have everything working kind of I just keep getting error.
IndexOutOfRangeException: Array index is out of range.
ScrollingBackground.Update () (at Assets/ScrollingBackground.cs:54)
When I change the parallax speed while in game mode everything works correctly. But it does not work when on start up. Can anyone help me? Thanks
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ScrollingBackground : MonoBehaviour
{
public bool scrolling, paralax;
public float backgroundSize;
public float paralaxSpeed;
public GameObject player;
private Transform[] layers;
// private float playerTransform;
private float viewZone = 10;
private float lastCameraX;
private float lastCameraZ;
private int leftIndex;
private int rightIndex;
private void Start()
{
// playerTransform =
lastCameraX = player.transform.position.x;
lastCameraZ = player.transform.position.z;
layers = new Transform[transform.childCount];
for (int i = 0; i < transform.childCount; i++)
layers[i] = transform.GetChild(i);
leftIndex = 0;
rightIndex = layers.Length - 1;
}
private void Update()
{
if(paralax)
{
float deltaX = player.transform.position.x - lastCameraX;
float deltaZ = player.transform.position.z;
transform.position += Vector3.right * (deltaX * paralaxSpeed);
transform.position += Vector3.forward * 0f;
}
lastCameraX = player.transform.position.x;
lastCameraZ = player.transform.position.z;
if (scrolling)
{
if (player.transform.position.x < (layers[leftIndex].transform.position.x + viewZone))
ScrollLeft();
if (player.transform.position.x > (layers[rightIndex].transform.position.x - viewZone))
ScrollRight();
}
}
private void ScrollLeft()
{
int lastRight = rightIndex;
layers[rightIndex].position = Vector3.right * (layers[leftIndex].position.x - backgroundSize);
leftIndex = rightIndex;
rightIndex--;
if (rightIndex < 0)
rightIndex = layers.Length - 1;
}
private void ScrollRight()
{
int lastLeft = leftIndex;
layers[leftIndex].position = Vector3.right * (layers[rightIndex].position.x + backgroundSize);
rightIndex = leftIndex;
leftIndex++;
if (leftIndex == layers.Length)
leftIndex = 0;
}
}