Advertisement
Guest User

Untitled

a guest
Nov 8th, 2018
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.10 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class BackgroundScrollController : MonoBehaviour {
  6.  
  7.     [SerializeField] GameObject roadGameObject;
  8.     [SerializeField] float scrollSpeed = 5f;
  9.     [SerializeField] float timeBetweenRoads = .5f;
  10.     [SerializeField] float destroyAfterSeconds = 2f;
  11.  
  12.     // Use this for initialization
  13.     void Start () {
  14.         StartCoroutine(BackgroundScroll());
  15.     }
  16.    
  17.     private IEnumerator BackgroundScroll()
  18.     {
  19.         // Instantiates road as a gameobject, sets the velocity to scroll downwards, and then destroys it after a set amount of time.
  20.         GameObject road = Instantiate(roadGameObject, new Vector3 (transform.position.x, transform.position.y, 5), Quaternion.identity) as GameObject;
  21.         road.transform.parent = transform;
  22.         road.GetComponent<Rigidbody2D>().velocity = new Vector2(0, -scrollSpeed);
  23.         Destroy(road, destroyAfterSeconds);
  24.  
  25.         // Waiting to restart the coroutine
  26.         yield return new WaitForSeconds(timeBetweenRoads);
  27.  
  28.         StartCoroutine(BackgroundScroll());
  29.     }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement