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 dvineinfekt · Apr 25, 2012 at 10:49 PM · movementspawntopdown

First game; Game Manager Question; Spawning enemies!

Working on my first game in Unity 3, a top-down shooter. Nothing too fancy. I have 8 off-screen spawn points littered about...I'm not really sure where to go from here. I can't really tell "How" I should do this. Obviously, theres more than one way, but I don't really know where to start.

I have the spawn points put into an array with GameObject.FindGameObjectsWithTag("spawnpoint"); but that's about it. How do I utilize my new array? What can I do with it? Suppose I wanted to send an enemy from a random spawnpoint listed in the array. Where would I call that? In the Update function, perhaps? I hope someone can point me in the right direction!

What I want to have happen is: Be able to choose a spawn point for a given enemy (or even have a loop to cycle through points and spawn them for me) and have it fly toward the player.

Also: I'm coding in javascript.

PS: I'm not necessarily asking for someone to simply write some code - if someone could help explain by way of example and sample code, that would be much better :) Thanks!

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

1 Reply

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

Answer by aldonaletto · Apr 26, 2012 at 01:57 AM

A simple trick is to create an empty game object, call it "Enemies" and attach the spawning script to it. To keep control of how many enemies are currently alive, child each enemy created to "Enemies" and use transform.childCount to know how many they are:

var enemyPrefab: Transform; var maxEnemiesAlive: int = 10; var maxEnemies: int = 30; private var totalEnemies: int; private var spawnPoints: GameObject[]; private var player: Transform;

function Start(){ spawnPoints = GameObject.FindGameObjectsWithTag("spawnpoint"); player = GameObject.FindWithTag("Player").transform; totalEnemies = maxEnemies; // define how many enemies the level has }

function Update(){ // when some enemy is killed and total enemies isn't zero yet... if (transform.childCount < maxEnemiesAlive && totalEnemies > 0){ // draw a random spawn point: var pos = spawnPoints[Random.Range(0, spawnPoints.length)].transform.position; // find a rotation to look at the player: var rot = Quaternion.LookRotation(player.position - pos); // create the new enemy facing the player: var enemy = Instantiate(enemyPrefab, pos, rot); // child it to Enemies (this object): enemy.parent = transform; totalEnemies--; // decrement the remaining enemies } } This basic code instantiate enemies whenever the current population falls below maxEnemiesAlive, and create them facing the player. You can add a script to the enemy prefab to make it go forward, or chase the player. You can also add a Destroy(enemy.gameObject, someTime) right after its creation, so it will die when the specified time has ellapsed.

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 dvineinfekt · May 03, 2012 at 02:45 PM 0
Share

That's an interesting idea...I never thought of this! I might take the idea and see if I can't work it into what I have. I forgot I posted this question, but the solution looks much cleaner than what I have and would probably function lot better as well!

If nothing else, I learned something really cool here! Thank you Aldo!

avatar image aldonaletto · May 04, 2012 at 01:58 PM 0
Share

On more thing: the rotation calculated may create tilted enemies when the player and the spawn point heights are different. To solve this, calculate the direction vector and zero its Y component to get only the horizontal direction:

 // find a rotation to look at the player:
 var dir = player.position - pos;
 dir.y = 0; // kill the vertical component
 var rot = Quaternion.LookRotation(dir);

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

Issue with manual collision and mobs spawning... 1 Answer

Top down 2d movement without being affected by rotation 2 Answers

More efficient way to spawn enemies in my game...? 2 Answers

Whats the best way to move a 2d rigidbody? 2 Answers

Teleporting device? 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