I am trying to have a parallax effect as well as infinite movement.
The Problem:
The movement works and the infinite scrolling works and the parallax effect works; however, after reaching a certain point it seems the background objects refuse to transform their position , thus infinite scrolling fails.
When Removing the parallax effect code everything seems o work as intended for the infinite scrolling.
// infinite scrolling fails
// ParallaxEffect and Infinite Scrolling Script
using UnityEngine;
using System;
public class ParallaxEffect : MonoBehaviour
{
[SerializeField]
Camera cam;
[SerializeField]
SpriteRenderer sprt;
Vector2 startPos;
Vector2 objSize;
Vector2 camSize;
public float parallaxEffect;
private void Awake()
{
sprt = GetComponent<SpriteRenderer>();
}
private void Start()
{
// Start position of object
startPos = new Vector2(transform.position.x, transform.position.y);
objSize = new Vector2(sprt.bounds.size.x, sprt.bounds.size.y);
camSize = new Vector2(cam.orthographicSize * 2f * cam.aspect, cam.orthographicSize * 2f);
}
private void Update()
{
// Parallax Effect
Vector2 dist = new Vector2(cam.transform.position.x * parallaxEffect, cam.transform.position.y * parallaxEffect);
transform.position = new Vector3(startPos.x + dist.x, startPos.y + dist.y, transform.position.z);
// Infinite Scroll
Vector2 camPos = new Vector2(cam.transform.position.x, cam.transform.position.y);
Vector2 camSize = new Vector2(cam.orthographicSize * 2f * cam.aspect, cam.orthographicSize * 2f);
Vector2 objPos = new Vector2(transform.position.x, transform.position.y);
Vector2 objSize = new Vector2(sprt.bounds.size.x, sprt.bounds.size.y);
// Right of Camera
if(objPos.x > camPos.x)
{
// Check obj left edge greater than cam right edge
// obj is out of camera view from the right
if((objPos.x - (objSize.x / 2f)) > (camPos.x + (camSize.x / 2f)))
{
transform.position = new Vector2(objPos.x - (objSize.x * 3f), transform.position.y);
}
}
// Left of Camera
else if (objPos.x <= camPos.x)
{
// Check obj right edge less than cam left edge
// obj is out of camera view from the left
if ((objPos.x + (objSize.x / 2f)) <= (camPos.x - (camSize.x / 2f)))
{
transform.position = new Vector2(objPos.x + (objSize.x * 3f), transform.position.y);
}
}
// Above Camera
if(objPos.y > camPos.y)
{
// Check obj bottom edge greater than cam top edge
// obj is out of camera view from the top
if ((objPos.y - (objSize.y / 2f)) > (camPos.y + (camSize.y / 2f)))
{
transform.position = new Vector2(transform.position.x, objPos.y - (objSize.y * 2f));
}
}
// Below Camera
else if(objPos.y <= camPos.y)
{
// Check obj top edge less than cam bottom edge
// obj is out of camera view from the bottom
if ((objPos.y + (objSize.y / 2f)) <= (camPos.y - (camSize.y / 2f)))
{
transform.position = new Vector2(transform.position.x, objPos.y + (objSize.y * 2f));
}
}
}
}
Thank you for your consideration and help with my issue. I appreciate it very much!
I apologize for the limited screenshots etc. I am a ne user am only allowed to include 1 embedded media.