Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by cdm89 · Feb 08, 2016 at 10:17 AM · 2d gamespawning

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 scenealt text

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);
                     }
                 }
             }
         }
     }
 }


unity.jpg (42.0 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges