I’m noob in this place, hope I don’t make a mistake posting.
First of all apologise my English writing.
I can’t find anything about it, or at least not as accurate .
How can I manage multiple backgrounds?
I’m trying to make a running game. In that game i want to change the background after every 120 seconds or 30 points, like in game like “Jetpack joyride” or “Flappy bird”(day and night) .
Generally, it will involve learning to script, Maybe placing colliders (as triggers) and turning Sprite images on and off. But those tutorials cover everything you will need.
To get you started alongside the tutorials, here is a script that will change a sprite on a SpriteRenderer:
using UnityEngine;
public class ChangeBackground : MonoBehaviour
{
public SpriteRenderer spriteRenderer;
public Sprite[] backgrounds;
public float timerInterval = 30f;
private float timer;
private int currentSpriteIndex;
private void Start()
{
SetDefaultBackground();
ResetTimer();
}
private void SetDefaultBackground()
{
// Error checking
if (spriteRenderer == null)
{
Debug.LogError("No SpriteRenderer set on ChangeBackground script.");
return;
}
if (backgrounds.Length == 0)
{
Debug.LogError("No backgrounds set on ChangeBackground script.");
return;
}
currentSpriteIndex = 0;
spriteRenderer.sprite = backgrounds[currentSpriteIndex];
}
private void ResetTimer()
{
timer = timerInterval;
}
private void Update()
{
if (timer <= 0f)
{
ResetTimer();
OnTimerFinished();
}
timer -= Time.deltaTime;
}
private void OnTimerFinished()
{
// Increment the index by one, but if we go beyond the number of items
// in the backgrounds array, wrap around and start counting from zero again.
currentSpriteIndex = (currentSpriteIndex + 1) % backgrounds.Length;
spriteRenderer.sprite = backgrounds[currentSpriteIndex];
}
private void OnValidate()
{
// Ensure timer interval stays positive
// when set through the inspector.
if (timerInterval <= 0f)
timerInterval = 0f;
}
}
Create a new 2D Sprite object in the scene and attach the ChangeBackground component.
Set the SpriteRenderer reference in the inspector and fill the array with background sprite images (make sure they are imported and marked as ‘Sprite’).
See the script cycling through the array, changing the background every x seconds.
Hey i’m looking for the same thing but with paralax effect on each of my background any idea of what is the best way?,Hi anyway for use multiple paralax background with the same idea?