Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Michael 8 · Mar 07, 2012 at 04:20 PM · spawnspawnpoint

Multiple spawn points (C#)

I'm trying to tackle the logic for the following setup:

I have multiple spawn points (bases) that can be destroyed and then become inactive. How many are there changes from level to level.

These spawn points spawn enemies randomally until a certain number of enemies exists in the world (say 20). It would then not spawn any more enemies until some of the existing ones are destroyed by the player.

Are there any existing tutorials for something like that (preferably C#)? Any pointers will be much appreciated.

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 aldonaletto · Mar 07, 2012 at 05:07 PM

A simple solution to control the enemy population is to create an empty object - let's call it "Enemies" - reset its position and rotation, then child every created enemy to it. The spawning script is attached to the Enemies object, and contains an array with the bases. The enemy count is the Enemies childCount property, and is compared to maxEnemies each Update: when it's lower than the maxEnemies value, one of the bases is randomly selected, and the new enemy is instantiated at that position and childed to Enemies. If the base has been destroyed, its reference is null, and the script does nothing - a new base will be randomly selected in the next Update.

public Transform[] bases; // drag the bases here public float offSet = 1.5f; // height at which the enemy is created public Transform enemyPrefab; public int maxEnemies = 20;

void Update(){ if (transform.childCount < maxEnemies){ // if less than maxEnemies... // select a random base: Transform base = bases[Random.Range(0, bases.Length)]; if (base){ // if the selected base doesn't exist, try again next Update // calculate position: base location plus vertical offset: Vector3 pos = base.position + offSet * Vector3.up; // instantiate the enemy... Transform enemy = Instantiate(enemyPrefab, pos, Quaternion.identity) as Transform; // and child it to Enemies: enemy.parent = transform; } } } NOTE: I usually write in JS, thus the code above may have stupid C# errors!

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 Julien-Lynge · Mar 07, 2012 at 04:40 PM

@Michael_8

I would start by looking at the search results on this site:

http://answers.unity3d.com/search.html?redirect=search%2Fsearch&q=multiple+spawn+point

A number of which have example code and ideas.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

UNET - Server is spawned in correct spawn position but players are spawned "half way" 0 Answers

Prefab to spawn on right spot 1 Answer

Can't set timer 1 Answer

3 spawn positions but one is ignored and i dont know why 2 Answers

What is the best way to spawn enemies? 3 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