Last active
August 29, 2015 14:21
-
-
Save marcospgp/5602dc2fa7b7e2cf4945 to your computer and use it in GitHub Desktop.
Unity 2D Horizontal Sprite Scroller
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public class ScrollingBackground : MonoBehaviour { | |
// Here's what this script does, step by step: | |
// Duplicates the object | |
// Scrolls both at a given scrollSpeed | |
// When an object's full length has been scrolled, it is placed on the duplicate's original position | |
// Last step is repeated for both objects indefinitely | |
public float scrollSpeed = 0.5F; // Scroll speed in units per second | |
private Vector3 originalPosition; | |
private float originalXPosition; | |
private float maxX; | |
private float minX; | |
private float spriteWidth; | |
private bool isDuplicate; | |
private GameObject duplicate; | |
void Start () { | |
// Get this object sprite's width and X position | |
spriteWidth = GetComponent<SpriteRenderer>().sprite.bounds.size.x; | |
originalPosition = transform.position; | |
originalXPosition = transform.position.x; | |
if (!isDuplicate) { | |
// Set the max and min X values (the duplicate has these passed to it) | |
maxX = originalXPosition + spriteWidth; | |
minX = originalXPosition - spriteWidth; | |
// Instantiate a duplicate of this object on its right | |
Vector3 duplicatePosition = transform.position; | |
duplicatePosition.x += spriteWidth; | |
duplicate = (GameObject) Instantiate(gameObject, duplicatePosition, Quaternion.identity); | |
// Tell duplicate to not duplicate itself | |
duplicate.GetComponent<ScrollingBackground>().DefineAsDuplicate(minX, maxX); | |
} | |
} | |
void Update () { | |
// Wait for duplicate to be instantiated | |
if (!isDuplicate && !duplicate) { | |
return; | |
} | |
float offset = Time.deltaTime * scrollSpeed; | |
float newX = transform.position.x; | |
newX -= offset; | |
if (newX <= minX) { | |
// Move sprite forwards | |
// Assume we have overshot minX and add difference to offset | |
offset += minX - transform.position.x; | |
Vector3 newPosition = originalPosition; | |
newPosition.x = maxX - offset; | |
transform.position = newPosition; | |
} else { | |
// Move sprite backwards | |
Vector3 newPosition = transform.position; | |
newPosition.x -= offset; | |
transform.Translate(Vector3.left * offset); | |
} | |
} | |
void OnDestroy () { | |
Destroy(duplicate); | |
} | |
// Called if this instance is the duplicate | |
public void DefineAsDuplicate (float minX, float maxX) { | |
// Prevent creation of another duplicate | |
this.isDuplicate = true; | |
if (duplicate) { | |
Destroy(duplicate); | |
} | |
this.minX = minX; | |
this.maxX = maxX; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment