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 TheFrankman123 · Apr 27, 2012 at 05:03 PM · randomspawnspawning

Random Spawning of Objects at Random Locations

Hello, I have thrown together a random spawning script, however i would like it to be a bit more complicated. At the moment i have a spawn point being chosen at random however, that the chose spawn point will only ever spawn the same object. Here is what i have so far:

 var timer : float = 0.0;
 var spawning : boolean = false;
 var meteor1 : Rigidbody;
 var meteor2: Rigidbody;
 var meteor3 : Rigidbody;
 
 var spawn1 : Transform;
 var spawn2 : Transform;
 var spawn3 : Transform;
  
 function Update () {
     
  if(!spawning){
   timer += Time.deltaTime;
  }
 
  if(timer >= 1.5){
   Spawn();
  }
 }
  
 function Spawn(){
 
  spawning = true;
  timer = 0;
  
  var randomPick : int = Mathf.Abs(Random.Range(1,4));
  var location : Transform;
  var whichObj : Rigidbody;
  
  if(randomPick == 1){
   location = spawn1;
   whichObj = meteor1;
   Debug.Log("Chose pos 1");
  }
  else if(randomPick == 2){
   location = spawn2;
   whichObj = meteor2;
   Debug.Log("Chose pos 2");
  }
  else if(randomPick == 3){
   location = spawn3;
   whichObj = meteor3;
   Debug.Log("Chose pos 3");
  }
 
 
  var meteorToMake : Rigidbody = Instantiate(whichObj, location.position, location.rotation);
   meteorToMake.AddForce(-Vector3(0,0,500));
  
  yield WaitForSeconds(1);
  spawning = false;
 }

What I would like is to also choose the object that spawns from the spawn point randomly... Any ideas?

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
1

Answer by bompi88 · Apr 27, 2012 at 06:08 PM

Try this:

 var timer : float = 0.0;
 var spawning : boolean = false;
 var meteors : Rigidbody[];
 
 var spawnPoints : Transform[];
 
 function Update () {
 
  if(!spawning){
   timer += Time.deltaTime;
  }
 
  if(timer >= 1.5){
   Spawn();
  }
 }
 
 function Spawn(){
 
  spawning = true;
  timer = 0;
 
  var randomSpawnPick : int = Mathf.Abs(Random.Range(0,spawnPoints.length-1));
  var randomRigidPick : int = Mathf.Abs(Random.Range(0,meteors.length-1));
  var location : Transform;
  var whichObj : Rigidbody;
 
   location = spawnPoints[randomSpawnPick];
   whichObj = meteors[randomRigidPick];
 
  var meteorToMake : Rigidbody = Instantiate(whichObj, location.position, location.rotation);
   meteorToMake.AddForce(-Vector3(0,0,500));
 
  yield WaitForSeconds(1);
  spawning = false;
 }
Comment
Add comment · Show 3 · 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 TheFrankman123 · Apr 27, 2012 at 06:41 PM 0
Share

what's all this about 'var thisG : GameObject; var enemies : GameObject[]; var closestEnemy : GameObject;'

avatar image bompi88 · Apr 27, 2012 at 11:41 PM 0
Share

Sorry, you can delete those.. I pasted it in an old file, for editing. Some of the old stuff came along.. Are you sure you don´t want any enemies in your game :-P hehe..

avatar image TheFrankman123 · May 02, 2012 at 04:09 PM 0
Share

Haha, i have plenty of enemies but no random spawning required. Was just wondering if i was missing something.

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

How do I make a game object spawn several times in semi-random locations? 1 Answer

Enemies spawn on top of each other(C#)(Unity) 1 Answer

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

How to spawn random buildings 5 Answers

Creating a random number of spawning items on collision 2 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