-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBGMover
More file actions
79 lines (73 loc) · 2.4 KB
/
Copy pathBGMover
File metadata and controls
79 lines (73 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BGMover : MonoBehaviour {
public float moveSpeed = 5f;
private GameObject[] sideBounds;
private float cameraY;
private float boundHeight;
public GameObject[] enemies;
public GameObject[] spawnPositions;
// Use this for initialization
void Awake () {
sideBounds = GameObject.FindGameObjectsWithTag("sidebound");
cameraY = Camera.main.gameObject.transform.position.y - 15f;
boundHeight = GetComponent<BoxCollider2D>().bounds.size.y;
}
// Update is called once per frame
void Update () {
Move();
Reposition();
}
void Move()
{
Vector3 temp = transform.position;
temp.y -= moveSpeed * Time.deltaTime;
transform.position = temp;
}
void Reposition()
{
if (transform.position.y < cameraY)
{
float highestBoundsY = sideBounds[0].transform.position.y;
for (int i = 1; i < sideBounds.Length; i++)
{
if (highestBoundsY < sideBounds[i].transform.position.y)
{
highestBoundsY = sideBounds[i].transform.position.y;
}
}
Vector3 temp = transform.position;
temp.y = highestBoundsY + boundHeight -1f;
transform.position = temp;
SpawnEnemies();
}
}
void SpawnEnemies()
{
if(Random.Range(0,10)>0)
{
int randomEnemyIndex = Random.Range(0, enemies.Length);
if (randomEnemyIndex == 0)
{
Instantiate(enemies[randomEnemyIndex], new Vector3(0f, transform.position.y, 3f), Quaternion.identity);
}
else
{
GameObject enemyObj = Instantiate(enemies[randomEnemyIndex]);
Vector3 enemyScale = enemyObj.transform.localScale;
if (Random.Range(0, 2) > 0)
{
enemyObj.transform.position = spawnPositions [0].transform.position;
enemyScale.x = -Mathf.Abs(enemyScale.x);
}
else
{
enemyObj.transform.position = spawnPositions[1].transform.position;
enemyScale.x = Mathf.Abs(enemyScale.x);
}
enemyObj.transform.localScale = enemyScale;
}
}
}
}