- Home /
Looping background
So I am new to Unity and I am trying to make an endless runner. The problem is, I want my background image to loop and look like it is moving. I tried some coding myself but it didn't work and with a little bit of googling I found a guy using a code for the background just like this:
public class LoopingBackground : MonoBehaviour
{
public float backgroundSpeed; //How fast the background is moving
public Renderer backgroundRenderer;
// Start is called before the first frame update
void Start()
{
backgroundRenderer.material.mainTextureOffset += new Vector2(backgroundSpeed * Time.deltaTime, 0f);
}
}
But the thing is the background just looks like it stays still after I implemented the code above. The background sprite's wrap mode is on repeat, it's a child of the GameManager empty object, camera is a child of the GameManager to make the camera move on the x axis endlessly. If you need any more information tell me I can provide.
This is the guy I found on the youtube. At 9:20, you can see the background movement. That's exactly what I want to do.
Answer by tyruji · Jul 23, 2021 at 10:52 PM
You are not updating the offset every frame, you are offsetting it only once, since you call it in the Start message. The solution would be to replace Start (which is called before the first frame of Update) to Update (which runs every frame). Good luck with your endless runner!
Your answer
![](https://koobas.hobune.stream/wayback/20220613040723im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Endless 2D background 4 Answers
Instantiate objects in a specific order 2 Answers
How to make looped background?(Character enters left side and appears from right) 2 Answers
Endless runner background looping delayed. 0 Answers
Infinite Background 1 Answer