Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class BackgroundScrollController : MonoBehaviour {
- [SerializeField] GameObject roadGameObject;
- [SerializeField] float scrollSpeed = 5f;
- [SerializeField] float timeBetweenRoads = .5f;
- [SerializeField] float destroyAfterSeconds = 2f;
- // Use this for initialization
- void Start () {
- StartCoroutine(BackgroundScroll());
- }
- private IEnumerator BackgroundScroll()
- {
- // Instantiates road as a gameobject, sets the velocity to scroll downwards, and then destroys it after a set amount of time.
- GameObject road = Instantiate(roadGameObject, new Vector3 (transform.position.x, transform.position.y, 5), Quaternion.identity) as GameObject;
- road.transform.parent = transform;
- road.GetComponent<Rigidbody2D>().velocity = new Vector2(0, -scrollSpeed);
- Destroy(road, destroyAfterSeconds);
- // Waiting to restart the coroutine
- yield return new WaitForSeconds(timeBetweenRoads);
- StartCoroutine(BackgroundScroll());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement