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);
}
}
im just not familiar with that setup