• 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
-2
Question by 3eBpA · Dec 21, 2012 at 12:55 AM · randomspawn

Random spawn

Hi guys , can you help me with random spawn enemyes ?

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 cagezero · Dec 21, 2012 at 04:48 AM 1
Share

Can you be a little more specific? Do you need help selecting a random enemy to place at a random time and location? Are there constraints? Do you already have code you are working with? A little specificity goes a long way ;)

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Azound · Dec 21, 2012 at 06:08 AM

One way to accomplish this is to create some prefabs that are ready.

Then create a new script with references to the prefabs and a Spawn method. Then you just call Spawn to create a new object with a random orientation located somewhere between the origin and maxPos.

 public Vector3 maxPos;
 public Object myPrefab;
 
 GameObject Spawn() {
     return Object.Instantiate(myPrefab, 
                               new Vector3(Random.value * maxPos.x,                                                      
                                           Random.value * maxPos.y, 
                                           Random.value * maxPos.z), 
                               Random.rotation);
 }
 
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 Zarenityx · Jan 01, 2013 at 05:33 PM

Azound provided a good way to put objects at a random position, but if you were looking for a way to make a random type of enemy at a given point, here is the script:

 #pragma strict
 var ObjectsToSpawn : GameObject[];
 var PlaceToSpawn : Vector3;
 private var Selected : int;
 
 function Spawn(){
 Selected = Random.Range(0,ObjectsToSpawn.Length);
 Instantiate(ObjectsToSpawn[Selected],PlaceToSpawn,Quaternion.identity);
 }

Simply create the objects you want to spawn and place them in the array, then specify the place they will spawn. Then just call Spawn() from a script. If you want an object to be spawned more often, simply put it in the array multiple times.

I combined both ideas in the following script, so when Spawn() is called, a random object will be spawned at a random point in a given area with boundaries you determine.

 #pragma strict
 var ObjectsToSpawn : GameObject[];
 var MinPosition : Vector3;
 var MaxPosition : Vector3;
 private var PlaceToSpawn : Vector3;
 private var Selected : int;
 function Spawn(){
 Selected = Random.Range(0,ObjectsToSpawn.Length);
 PlaceToSpawn.x = Random.Range(MinPosition.x,MaxPosition.x);
 PlaceToSpawn.y = Random.Range(MinPosition.y,MaxPosition.y);
 PlaceToSpawn.z = Random.Range(MinPosition.z,MaxPosition.z);
 Instantiate(ObjectsToSpawn[Selected],PlaceToSpawn,Quaternion.identity);
 }


Now, create prefabs and add them to the ObjectsToSpawn array as before, but instead of specifying 1 Vector3 to be the Spawnpoint, specify the minimum position and the maximum position. These can be thought of as opposite vertices of a cube, and any object in the array can be put in any point in the cube when Spawn() is called. This is great for creating enemy spawners. If you want to exclude an axis, simply set the minimum and maximum values of that axis to the same value.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

13 People are following this question.

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

Related Questions

How to randomly replace the main platform with additional ones? 0 Answers

Drop item from airplane 1 Answer

AI spawning areas. What are the ways to make them? 1 Answer

Spawn object from Random Vector3 in array 0 Answers

Endless/infinite runner random enemy spawning multi lane 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