PLEASE IGNORE HERE AND GO DOWN
I’m working on a parallax background and wrote this code. but the code I commented “this loop” loops always. I controlled variables for multiple times and here a set of them when I paused game
changePos[0] = -1919
changePos[1] = -1897
transform.position.x = -1905
for this values loop must not loop but it’s. I’m working on this for 1 day and couldn’t find the problem
using UnityEngine;
public class Parallax_Cont : MonoBehaviour
{
public SpriteRenderer spriteRenderer;
public Transform camTrans;
public float spriteWidth;
public float parallaxAmount = 0.5f;
public float camInitialPosX;
float spriteInitialX;
public float[] changePos = new float[2];
void Start()
{
camTrans = Camera.main.transform;
spriteRenderer = GetComponent<SpriteRenderer>();
spriteWidth = spriteRenderer.bounds.size.x;
spriteInitialX = transform.position.x;
camInitialPosX = Camera.main.transform.position.x;
}
void FixedUpdate()
{
CheckForBound();
MoveParallax();
}
void CheckForBound()
{
if(transform.position.x > changePos[1])
{
transform.position = new Vector3(changePos[1] + spriteWidth/2, transform.position.y, transform.position.z);
spriteInitialX = changePos[1];
Debug.Log("Upper Limit Reached");
ChangeBounds();
}
if(transform.position.x < changePos[0]) //THIS LOOP
{
transform.position = new Vector3(changePos[0] - spriteWidth/2, transform.position.y, transform.position.z);
spriteInitialX = changePos[0];
Debug.Log("Bottom Limit Reached");
ChangeBounds();
}
}
void MoveParallax()
{
float moveAmount = camTrans.position.x - camInitialPosX;
transform.Translate(new Vector3(camTrans.position.x - camInitialPosX, 0, 0 ) * (1-parallaxAmount));
camInitialPosX = camTrans.position.x;
}
void ChangeBounds()
{
changePos[0] = spriteInitialX - spriteWidth * parallaxAmount;
changePos[1] = spriteInitialX + spriteWidth * parallaxAmount;
}
}