I need help with my scrolling background

I am currently working on a platformer and I was working a parallax background however The three sets of background images are all being spawned on top of each other and the background isn’t looping like it’s supposed to.

this is the tutorial I used:

here’s my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Parralax : MonoBehaviour
{
    private float length, startpos;
    public GameObject cam;
    public float parallaxEffect;
    // Start is called before the first frame update
    void Start()
    {
        startpos = transform.position.x;
        length = GetComponent<SpriteRenderer>().bounds.size.x;
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        float temp = (cam.transform.position.x * (1-parallaxEffect));
        float dist = (cam.transform.position.x * parallaxEffect);

        transform.position = new Vector3(startpos = dist, transform.position.y, transform.position.z);

        if(temp > startpos + length) startpos += length;
        else if (temp < startpos - length) startpos -= length;
    }
}

When doing a tutorial, always double check your code vs theirs.

2 Likes