1 using System.Collections;
2 using
System.Collections.Generic;
3 using
UnityEngine;
4
5 public
class ForeGroundSpawn : MonoBehaviour {
6
7   
// private GameObject[] backgrounds;
8     
private GameObject[] grounds;
9
10     
private float lastGroundX;
11     
private float lastBgX;
12
13     
void Awake () {
14         grounds = GameObject.FindGameObjectsWithTag(
"Foreground");
15  
// backgrounds = GameObject.FindGameObjectsWithTag("Background");
16
17         lastGroundX = grounds[
0].transform.position.x;
18 // lastBgX = backgrounds[
0].transform.position.x;
19
20         
for (int i = 1; i < grounds.Length; i++)
21         {
22             
if (lastGroundX < grounds[i].transform.position.x)
23             {
24                 lastGroundX = grounds[i].transform.position.x;
25             }
26         }

27 /*
28         
for (int i = 1; i < backgrounds.Length; i++)
29         {
30             
if (lastBgX < backgrounds[i].transform.position.x)
31             {
32                 lastBgX = backgrounds[i].transform.position.x;
33             }
34         } */

35     }
36     
37     
void Update () {
38         
39     }
40
41     
private void OnTriggerEnter2D(Collider2D target)
42     {
43         
if (target.tag == "Foreground")
44         {
45             Vector3 temp = target.transform.position;
46             
float width = ((BoxCollider2D)target).size.x;
47
48             temp.x = lastGroundX + width;
49             target.transform.position = temp;
50             lastGroundX = temp.x;
51         }
52
53     
/* else if (target.tag == "Background")
54         {
55             Vector3 temp = target.transform.position;
56             
float width = ((BoxCollider2D)target).size.x;
57
58             temp.x = lastBgX + width;
59             target.transform.position = temp;
60             lastBgX = temp.x;
61         } */

62     }
63 }


Gõ tìm kiếm nhanh...