MIF_E31221325/Assets/Sprites/Space Shooter Template FREE/Scripts/RepeatingBackground.cs

27 lines
856 B
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using UnityEngine;
/// <summary>
/// This script attaches to Background object, and would move it up if the object went down below the viewport border.
/// This script is used for creating the effect of infinite movement.
/// </summary>
public class RepeatingBackground : MonoBehaviour
{
[Tooltip("vertical size of the sprite in the world space. Attach box collider2D to get the exact size")]
public float verticalSize;
private void Update()
{
if (transform.position.y < -verticalSize) //if sprite goes down below the viewport move the object up above the viewport
{
RepositionBackground();
}
}
void RepositionBackground()
{
Vector2 groundOffSet = new Vector2(0, verticalSize * 2f);
transform.position = (Vector2)transform.position + groundOffSet;
}
}