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 kgoswami · Sep 13, 2011 at 03:20 AM · 2denemyspawnsidespawnpoint

What is the best way to spawn enemies?

  • My Game: 2d Side scroller like OMG pirates! game.

  • Problem: I need to know what is the best way to spawn enemies in code.

  • My solution: My solution is this for now, I will have transparent trigger objects on the path of the player. When the player passes over it, a script function does an instantiate on several spawnpoint game objects(invisible game objects on the map from which the enemy tanks get Instantiated). my enemy tanks are prefabs.

  • Problem in my solution: I will write an instantiate() line of code. This will generate one tank, I want to do 2 or 3 at a time. Does this mean I have to run a for loop and have multiple different named spawnpoints on the map?

Comment
Add comment · Show 1
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 BerggreenDK · Sep 13, 2011 at 08:41 AM 0
Share

$$anonymous$$aybe. I think yes. But its hard to tell without seeing a little of the actual code.

3 Replies

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

Answer by kgoswami · Sep 13, 2011 at 08:43 PM

Solution: I have used this . When Player character touches a trigger named "TriggerSpawn" spawnenemy object is spawned at a short distance from the player. I will later calubrate this distance so that the enemy is spawned offscreen from the iphone screen. It seems to b working for generating one enemy. I want to generate more, but ow the solution is simple, I just have to duplicate the trigger object. It has no script running, its just a long invisible line perpendicular to my character's line of path with an isTrigger checkbox ticked. self: Transform is a reference to the player object itself.

 var spawnenemy:Transform;
 var self:Transform;
 function OnTriggerEnter(collider: Collider)
 {
     if(collider.gameObject.name=="TriggerSpawn")
     {    
         var enemy=Instantiate( spawnenemy, transform.position + ( transform.right * 10), Quaternion.identity );
         //var enemy2=Instantiate( spawnenemy, transform.position + ( transform.left * 10), Quaternion.identity );
         enemy.GetComponent("TurretControlEnemyTank").LookAtTarget=self;
         //enemy2.GetComponent("TurretControlEnemyTank").LookAtTarget=self;
     }
     //spawnenemy.LookAtTarget=gameObject;
     //Debug.Log("SPAWN ENEMIES!");
 }
Comment
Add comment · 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
2

Answer by syclamoth · Sep 13, 2011 at 10:09 AM

If you wanted an easier way to implement a 'spawn points' system, you could use an array or list of Transform components, and then in the editor drag and drop each spawn point in to the list. In your script you would have the 'pos' Vector3 be determined something like

 public Transform[] spawnPoints;
 Vector3 position = spawnPoints[Random.Range(0, spawnPoints.Length)].position;

You could change the spawn direction the same way.

As for the player triggering the spawn, you could put your spawn script on the trigger volume, and then have it one-shot spawn a (possibly random) number of enemies at random spawnpoints from your list like this-

 for(int i; i < Random.Range(3, 10); i++)
 {
     Transform currentSpawn = spawnPoints[Random.Range(0, spawnPoints.Length)];
     Vector3 position = currentSpawn.position;
     Quaternion rotation = currentSpawn.rotation;
     Instantiate(prefab, position, rotation);
 }

This would spawn between 3 and 10 enemies at random points determined in the editor.

Of course, if you just wanted a random location in a circle, there's always Random.insideUnitCircle- you could state a centre point and spawn enemies around it like that.

Comment
Add comment · 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
0

Answer by timsk · Sep 13, 2011 at 09:41 AM

 // Instantiates a prefab in a grid
 
 var prefab : GameObject;
 var gridX = 5;
 var gridY = 5;
 var spacing = 2.0;
 
 function Start () {
     for (var y = 0; y < gridY; y++) {
         for (var x=0;x<gridX;x++) {
             var pos = Vector3 (x, 0, y) * spacing;
             Instantiate(prefab, pos, Quaternion.identity);
         }
     }
 } 

taken from unities instantiating prefabs discussion page Here

That is just one way to do it, another way would be to create empty game objects, called "Spawn Points" and attach the instantiate script to each one. This is much messier tho.

Comment
Add comment · 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

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

GameObject is spawned twice 0 Answers

Make enemy damage the player,Enemy do damage to player Script 1 Answer

Enemy reorienting for player 1 Answer

How to spawn a prefab on a trigger? 3 Answers

Phantom Instantiation at Origin 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