- Home /
Endless runner background looping delayed.
Hey guys! I'm having a problem while trying to loop my background. I have a script that is working just fine, the sprites are looping over and over, BUT, every time it reaches the end of the sprite to loop it again, it doesn't loop and you can see behind the sprite for like 3 seconds before the sprite actually looping.
Here's the script I'm using:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Parallax : 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 Update()
{
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;
}
}
Thank you for taking the time to read. If anyone knows why this happens please let me know!
Your answer
Follow this Question
Related Questions
Instantiate Loop Crashing 1 Answer
Creating delays without delaying the script 2 Answers
delay "after mouseclick" 0 Answers
Im confused,Background parallaxing looping is visible and not smooth? 0 Answers
Endless 2D background 4 Answers