#Parallax backgrounds with infinite scrolling

1 messages · Page 1 of 1 (latest)

dense surge
#

I'm a complete and utter beginner to unity, like started using it a week ago pretty much

I'm trying to implement an infinite looping background and parallax but i just cannot get it to work 😭😭😭😭
I've looked at so many yt tutorials and used AI to hell and back but absolutely no progress

Here's my parallax script:

using UnityEngine;

public class Parallax : MonoBehaviour
{
    public Camera cam;
    public Transform subject;
    public float length;
    Vector2 startPosition;
    float startZ;
  
    

    Vector2 travel => (Vector2)cam.transform.position - startPosition;
    float distanceFromSubject => transform.position.z - subject.position.z;
    float clippingPlane => (cam.transform.position.z + (distanceFromSubject > 0 ? cam.farClipPlane : cam.nearClipPlane));
    float parallaxFactor => Mathf.Abs(distanceFromSubject) / clippingPlane;

    public void Start()
    {
        
        startPosition = transform.position;
        startZ = transform.position.z;
        SpriteRenderer sr = GetComponent<SpriteRenderer>();
        length = sr.sprite.bounds.size.x * transform.localScale.x; // account for scale

        Transform[] children = GetComponentsInChildren<Transform>();
        foreach (Transform child in children)
        {
            if (child == transform) continue;
            float childX = Mathf.Round(child.localPosition.x / length) * length;
            child.localPosition = new Vector3(childX, child.localPosition.y, child.localPosition.z);
        }
        

    }

    public void LateUpdate()
    {


        if (travel.x > length)
        {
            startPosition.x += length;
        }
        else if (travel.x < -length)
        {
            startPosition.x -= length;
        }

        Vector2 newPos = startPosition + (travel * parallaxFactor);
        transform.position = new Vector3(newPos.x, newPos.y, startZ);


    }
}
#

basically what's happening right now is that
it's looping as it's supposed to
but it seems to shift the background a little too far back or a little too forward
like there's some weird offset

Please if anyone knows what on earth is wrong with it pleaseeee help me out 😭😭
spent way too long on something that's supposed to be simple

minor quiver
#

fwiw i'd recommend you stay away from AI as a beginner, you won't be able to learn properly

dense surge
#

Yeah you're right
I used it because I have no idea what else to do 😭

minor quiver
#

which object is this parallax script on?

dense surge
#

all the backgrounds but just the parent ones

#

I haven't named them well it's all the numbered layers

minor quiver
#

and those parents have the actual spriterenderers as children?

dense surge
#

I believe so? I just duplicated the original layer, removed the scripts and put it as a child to the original

#

one on either side

minor quiver
#

what objects have the spriterenderers here

dense surge
#

all of them!

minor quiver
#

what's the setup here exactly

#

show the hierarchy and some inspectors

dense surge
#

same thing for each layer

#

the parent layer has the parallax script and is in the centre
the child layers are +length and -length without the parallax script

minor quiver
#

so the parent is one of the repetitions of the background?

#

that doesn't sound right

dense surge
minor quiver
#

yeah that doesn't sound right

#

one setup ive seen would be the parent not rendering anything and handling the parallax so the entire background moves, then the children shifting around to make it loop

dense surge
#

oh interesting
so i have 3 children? and the parent is just an object of the same size?

minor quiver
#

objects don't really have inherent sizes

dense surge
#

why would it not work with the parent being part of the background?

minor quiver
#

hmm wait you're moving entire background to get the loop aren't you

dense surge
#

yep

minor quiver
#

i guess that could work thonk im just not familiar with that setup

dense surge
#

so far the looping part is working it's just shifting it too far? i think something with my code is off

dense surge
minor quiver
#

i'm more familiar with the one i described (though a bit convoluted)

i wasn't able to get a proper look before on mobile though, maybe i'll spot something

#

could you try debugging the travel/startPos when the conditions pass?

#

before/after the modification

#

and make sure length is as expected

dense surge
dense surge
verbal pine