How create a Scrolling Background with Parallax Effect in Unity
Here are the steps to create a scrolling background in Unity.
1. Add background to Scene
2. Duplicate (I did it twice and flipped the middle background on the X)
3. Set the new ones as childs of the 1st one
4. Align the backgrounds
5. Move the parent background (Y:-80)
6. Add the script
Infinite Scrolling Code
Use this code and add it to your Background.
[SerializeField] private float _scrollSpeed = -5f;
private Vector2 _startPos;
// Start is called before the first frame update
void Start()
{
_startPos = transform.position;
}
// Update is called once per frame
void Update()
{
float newPos = Mathf.Repeat(Time.time * _scrollSpeed, 80);
transform.position = _startPos + Vector2.up * newPos;
}
Parallax Effect
With creating multiple scrolling background with transparency you can create an parallax effect. Just set the scrolling speed of closer objects fast and the scrolling speed of far objects slow. Make sure to set order layer accordingly in Inspector. This will add some depth to you game making it look a lot better.