Spawning and Collecting an Enemy in a 2D game
Hello, I am a beginner at Unity, so I appologize if this is a pretty basic issue!
I am making a flappy bird clone (just to give you an idea of what the game is like - the camera moves etc.)
I wanted to make an "Evil Bird" that flies in the direction of the player and then gets collected when the evil bird is off screen. I cant figure out why the spawner does not spawn my evil birds. I was hoping that someone could send me off to the right direction to some resources, or give me some tips from personal experience
I want to reuse the same object, so once it hits the collecter and SetActive(false), then the spawner should make the evilbird SetActive(true) and then have it run through the scene, so on and so forth.
Attached is a drawing of what i have created in the scene
Here is the class for the spawning object
public class EvilBirdSpawner : MonoBehaviour {
[SerializeField]
private GameObject[] evilBird;
private float minY, maxY;
private float lastEvilBirdPositionX;
private float controlY;
void Awake()
{
controlY = 0;
setMinAndMaxY();
CreateEvilBird();
}
// Use this for initialization
void Start () {
}
void setMinAndMaxY()
{
Vector3 bounds = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, 0));
maxY = bounds.y - 0.5f;
minY = -bounds.y + 0.5f;
}
void Shuffle(GameObject[] arrayToShuffle)
{
for (int i = 0; i < arrayToShuffle.Length; i++)
{
GameObject temp = arrayToShuffle[i];
int random = Random.Range(i, arrayToShuffle.Length);
arrayToShuffle[i] = arrayToShuffle[random];
arrayToShuffle[random] = temp;
}
}
void CreateEvilBird()
{
Shuffle(evilBird);
float positionX = this.transform.position.x;
for (int i = 0; i < evilBird.Length; i++)
{
Vector3 temp = evilBird[i].transform.position;
temp.x = positionX;
temp.y = Random.Range(minY, maxY);
if(controlY == 0)
{
temp.y = Random.Range(0.0f, maxY);
controlY = 1;
}else if (controlY == 1)
{
temp.y = Random.Range(0.0f, minY);
controlY = 2;
}else if (controlY == 2)
{
temp.y = Random.Range(1.0f, maxY);
controlY = 3;
}else if(controlY == 3)
{
temp.y = Random.Range(2.0f, minY);
controlY = 0;
}
lastEvilBirdPositionX = positionX;
evilBird[i].transform.position = temp;
}
}
void OnTriggerEnter2D(Collider2D target)
{
if(target.tag == "Evil Bird")
{
if(target.transform.position.x == lastEvilBirdPositionX)
{
Shuffle(evilBird);
Vector3 temp = target.transform.position;
for(int i = 0; i < evilBird.Length; i++)
{
if (!evilBird[i].activeInHierarchy)
{
if(controlY == 0)
{
temp.y = Random.Range(0.0f, maxY);
controlY = 1;
}
else if (controlY == 1)
{
temp.y = Random.Range(0.0f, minY);
controlY = 2;
}
else if (controlY == 2)
{
temp.y = Random.Range(1.0f, maxY);
controlY = 3;
}
else if (controlY == 3)
{
temp.y = Random.Range(-1.0f, minY);
controlY = 0;
}
lastEvilBirdPositionX = temp.x;
evilBird[i].transform.position = temp;
evilBird[i].SetActive(true);
}
}
}
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612062922im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
can't instantiate gameobject 0 Answers
Spawning Path 0 Answers
Is there a better way to code this because this is causing a major amount of lag 0 Answers
2d Rogue like spawn issue 1 Answer
2D Rolling Ball 0 Answers