diff options
Diffstat (limited to 'nGJ2019/Assets/Scripts')
-rw-r--r-- | nGJ2019/Assets/Scripts/LevelScrolling.cs | 104 |
1 files changed, 57 insertions, 47 deletions
diff --git a/nGJ2019/Assets/Scripts/LevelScrolling.cs b/nGJ2019/Assets/Scripts/LevelScrolling.cs index 0122a69..fac0a13 100644 --- a/nGJ2019/Assets/Scripts/LevelScrolling.cs +++ b/nGJ2019/Assets/Scripts/LevelScrolling.cs @@ -4,66 +4,76 @@ using UnityEngine; public class LevelScrolling : MonoBehaviour
{
- public int UpdateRate = -5;
- public List<Transform> Obstacles;
- public Transform Background;
+ public int UpdateRate = -5;
+ public List<GameObject> Obstacles;
+ public Transform Background;
- private Vector3 initialBgPos;
- private float[] backgroundSize;
+ private Vector3 initialBgPos;
+ private float[] backgroundSize;
- // Start is called before the first frame update
- void Start()
- {
- ResizeBackground();
- }
+ // Start is called before the first frame update
+ void Start()
+ {
+ ResizeBackground();
+ }
+
+ // Update is called once per frame
+ void Update()
+ {
- // Update is called once per frame
- void Update()
- {
+ }
+
+ void FixedUpdate()
+ {
+ MoveBackground();
+ MoveObstacles();
+ DestroyObstacles();
+ }
- }
+ void ResizeBackground()
+ {
+ var sr = Background.GetComponent<SpriteRenderer>();
+ if (sr == null) return;
- void FixedUpdate()
- {
- MoveBackground();
- MoveObstacles();
- }
+ Background.localScale = new Vector3(1, 1, 1);
- private void ResizeBackground()
- {
- var sr = Background.GetComponent<SpriteRenderer>();
- if (sr == null) return;
+ var width = sr.sprite.bounds.size.x;
+ var height = sr.sprite.bounds.size.y;
- Background.localScale = new Vector3(1, 1, 1);
+ var worldScreenHeight = Camera.main.orthographicSize * 2.0;
+ var worldScreenWidth = worldScreenHeight / Screen.height * Screen.width;
- var width = sr.sprite.bounds.size.x;
- var height = sr.sprite.bounds.size.y;
+ var finalHeight = (float)(worldScreenHeight / height);
+ var finalWidth = (float)(worldScreenWidth / width);
- var worldScreenHeight = Camera.main.orthographicSize * 2.0;
- var worldScreenWidth = worldScreenHeight / Screen.height * Screen.width;
+ Background.localScale = new Vector3(finalHeight, finalHeight, 1);
- var finalHeight = (float)(worldScreenHeight / height);
- var finalWidth = (float)(worldScreenWidth / width);
+ var viewportX = Camera.main.ViewportToWorldPoint(new Vector3(0, 1, 0)).x;
+ Background.position = new Vector3(viewportX * -1, 0, 0);
+ initialBgPos = Background.position;
+ }
- Background.localScale = new Vector3(finalHeight, finalHeight, 1);
+ void MoveObstacles()
+ {
+ for (int i = 0; i < Obstacles.Count; i++)
+ {
+ Obstacles[i].transform.Translate(0.01f * UpdateRate, 0, 0);
+ }
+ }
- var viewportX = Camera.main.ViewportToWorldPoint(new Vector3(0, 1, 0)).x;
- Background.position = new Vector3(viewportX * -1, 0, 0);
- initialBgPos = Background.position;
- }
+ void DestroyObstacles()
+ {
+ if (Obstacles[0].transform.position.x < -30)
+ {
+ Destroy(Obstacles[0]);
+ Obstacles.RemoveAt(0);
+ }
+ }
- private void MoveObstacles()
- {
- foreach (var o in Obstacles)
+ void MoveBackground()
{
- o.Translate(0.01f * UpdateRate, 0, 0);
+ if (Background.position.x > -initialBgPos.x)
+ Background.Translate(0.01f * UpdateRate, 0, 0);
+ else Background.position = initialBgPos;
}
- }
-
- private void MoveBackground()
- {
- if (Background.position.x > -initialBgPos.x)
- Background.Translate(0.01f * UpdateRate, 0, 0);
- else Background.position = initialBgPos;
- }
}
|