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 /
avatar image
0
Question by unixty · Dec 06, 2015 at 09:05 PM · instantiateenemyinstantiationspace shooter

Shoot 'Em Up: Best way to instantiate enemies

First off, I'm not an english native speaker, so sorry in advance if it's a bit hard to read.

I am developing a 2D shoot'em'up and I've been working on boss fights, enemy scripts, designing backgrounds and other stuff.

If you've played any 2D game you may know that enemies don't appear at random, they do when the camera/player reaches a certain point in the map.

My idea was to create a GameObject and then put inside both camera and player and make it move towards different waypoints and as soon you reach one, an enemy appears with Instantiate and AddComponent.

The question is simple, if you are developing a 2d shoot'em'up/space shooter and you want to instantiate enemies at certain points, what would you do? Please, don't tell me "there are different ways depending on what you want", I want to know what you would do.

Here's what I have in mind, I haven't tried it out yet, just to show you my idea. Is that what you would do or something completely different?

 void Update(){
 
 if (move == true){
     cameraAndPlayer.transform.position = Vector3.MoveTowards (cameraAndPlayer.transform.position , waypoints[currentWaypoint].position, speed);
 }
 
     if (cameraAndPlayer.transform.position == waypoints[currentWaypoint].transform.position){
         if (currentWaypoint == 1){
             enemy = Instantiate (Resources.Load ("Prefabs/enemies/enemy1")) as GameObject;
             enemy.AddComponent<enemy1>();
         }
         else if (currentWaypoint == 2){
             enemy = Instantiate (Resources.Load ("Prefabs/enemies/enemy2")) as GameObject;
             enemy.AddComponent<enemy2>();
         }
 
         currentWaypoint++;
     }
 }

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Eno-Khaon · Dec 07, 2015 at 02:54 AM

If I were to approach this from the basis of pre-fabricated levels (or, more specifically, without random enemies unless specifically triggered during the level), I would probably take either of two approaches:

1) Instantiate enemies: I would create spawn groups either as prefabs or as data structures (and instantiate each enemy contained inside), then give each enemy a flight path pattern and shooting pattern of their own. Although there's the slight possibility it might have meaningful performance overhead with enough enemies generated at once, it's likely no worse than creating the tremendous number of projectiles they'll fire.

or...

2) Pre-place the enemies in a level: Create groups ahead of time and place them in the level, define all the characteristics of each enemy starting where they'll first become active, then deactivate their script. When the camera reaches a specific position (or game time reaches a specific time, depending on preference or emphasis on musical cues), activate the scripts of all enemies contained in the group, then call an "Activate()" function in the enemies to get them moving (or, alternately, to stagger them based on yield instructions). The key benefit to this option would be that you can define any script variables per ship in the editor without needing additional script control or prefabs per enemy.

I know you don't want to hear that it "depends on what you want", but I think this really depends more on what is necessary to get the result you need. If you're creating an endless game, you'll definitely need to Instantiate the enemies. If your game is meant to play out linearly, then it depends on how many enemies you're creating at a time, how many you're creating in total, and whether any one approach is giving you more trouble than another.

Comment
Add comment · Show 1 · Share
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
avatar image unixty · Dec 07, 2015 at 07:11 PM 0
Share

Thank you, I'll try out both ways using my waypoint system and then decide which is the best option. I'm pretty sure the waypoint/instantiation system is a common thing but pre-placing the enemies could be another good solution.

It would be cool if somebody who developed a "not random" space shooter could explain his method, maybe he used something completely different.

avatar image
0

Answer by hubi037 · Dec 07, 2015 at 04:51 PM

I would do this quite different. Each Waypoint would be a gameobject with a large sphere collider set to Trigger, denoting the area that the player needs to enter for an enemy to spawn.

Then you can have a script on the Waypoint that does this

 void OnTriggerEnter(Collider other)
 {
       //instantiate your enemy here
 }

Of course you should set up your collision layers in a way so that only the player can collide with your waypoint trigger.

Edit: You should probably disable the trigger after spawning your enemy so that this can only happen once.

Comment
Add comment · Show 2 · Share
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
avatar image Eno-Khaon · Dec 07, 2015 at 08:29 PM 0
Share

The only risk I see with that idea would be purely based on design intent. If it's based on the player reaching a specific region, then that means the player could rush forward and a dense wall of enemies spawns all at once, then they pull back again and no new enemies arrive for the maximum possible time.

It would certainly provide a different kind of experience, but a typical design for a shoot 'em up will send the enemies at a more-fixed rate, which the player would be unable to personally influence.

avatar image unixty Eno-Khaon · Dec 07, 2015 at 08:44 PM 0
Share

It could work if the camera+player gameobject.localposition is the trigger object, not the player, the camera will always be moving forwand (or diagonally), never backwards, as soon as it touches the trigger collider, it gets disabled.

Tne only problem is creating a single script for each collider ins$$anonymous$$d of just one level script that handles every waypoint or whatever used to instantiate the enemies.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Instantiate Explosion Position 2 Answers

Need help understanding scripted prefab behavior - when I click one, the script runs on ALL prefab instances, not just the one I clicked. 3 Answers

Simple Instantiate gameObject problem 3 Answers

How to instantly move a newly instantiated prefab? 2 Answers

How should I create/instantiate a skill/enemy/item 1 Answer


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