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 Yharooer · Jan 17, 2013 at 11:07 AM · javascriptprefabvector3spawn

Monster Spawner

Hence the title, I just want a monster spawner that spawns a monster every xxx frames. I pretty confident as making the "every xxx frames" part of it, but I have 7 vector3 variables and i want a monster to spawn at random at any one of those locations. I think I can make the code to choose the random of the 7 vector3 variables.

What I don't know how to do is IN CODE, 'spawn' or place a prefab in space USING CODE. (I'd prefer javascript, don't know a bit of C#.)

Then I think I know how to move the monster to the given location.

Anyone know how to do this, or at least point me in the right direction? Any help would be much appreciated. 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

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Imankit · Jan 17, 2013 at 11:26 AM

Use something like this..:

 var randPos = 0;
 
 // In your function or Update
 
 randPos = Random.Range(0,7);
 
 
 // When you want to position the monster out of those 7 positions say pos1,pos2,pos3,pos4,pos5,pos6,pos7, just call this function
 
 pos(randPos);
 
 // pos function
 function pos(a)
 switch(a){
 case 0 : monster.transform.position = pos1;
          break;
 case 1 : monster.transform.position = pos2;
          break;
 case 2 : monster.transform.position = pos3;
          break;
 case 3 : monster.transform.position = pos4;
          break;
 case 4 : monster.transform.position = pos5;
          break;
 case 5 : monster.transform.position = pos6;
          break;
 case 6 : monster.transform.position = pos7;
          break;
 
 }
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
1

Answer by cdrandin · Jan 23, 2013 at 06:19 AM

You could do something blunt like.

 public rate : int = 1;
 private lastTime : float;
 
 
 ...//Down the line where you have your monster.transform.position 
 
 for(var n = 0; n < rate; n++)
 {
     Instantiate(monster, monster.transform.position, monster.transform.rotation); 
     lastTime = Time.time;
 }
 
 //Simply create one per frame which imo is kind of fast, but is a very simple idea. Might want to play with the yield WaitForSeconds to add some delay.
 
 Then after Time has passed, let's say the variable rateTimer, is how many seconds till our rate will increase to spawn more at a time.
 soo...
 
 if((Time.time - lastTime) > rateTimer)
 {
     rate +=1; //so our rate increase after time has passed and will keep doing this up to infinity.
 }

The code isn't tested and is more a reflection of the idea.

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 cdrandin · Jan 23, 2013 at 06:20 AM 0
Share

Also, it is possible for the monster to spawn at the same position and if they have colliders are more likely to cause some problems unless you ignore collision with the monsters, which you can have a tag "monster"

avatar image Yharooer · Jan 23, 2013 at 06:48 AM 0
Share

Thanks heaps! Only one more thing, is it possible to use 'Instantiate' on another object, for example I create a "monster spawn" object and clone the monster object?

EDIT: Forget all of that. That's answered my quesition. Thanks!

avatar image cdrandin · Jan 23, 2013 at 06:50 AM 0
Share

It is never wise to Instantiate a copy of an object on the scene. It is why Unity has prefabs for organization. It is possible to answer your question, but I wouldn't do it.

avatar image
0

Answer by DayyanSisson · Jan 23, 2013 at 06:41 AM

What you need to do is assign the random positions the monsters could spawn in an array:

 var randomPositions: Transform[];

This will give you a dropdown list in the inspector where you can put as many random positions as you want. Next you want to call the function that will spawn the monsters:

 if(Input.GetButtonDown("Jump")){
    //Spawn monster code
 }

So if I hit space it will run the spawn monster code. Next is another variable I need to set up; how many monsters I want spawned:

 var numberOfMonstersToSpawn: int;

Next in your monster code, you want to run a while loop to run the monster code as many times as you need to spawn all the monsters:

 var i: int = 0;
 while(i < numberOfMonstersToSpawn){
    //Code here
    i++;
 }

So what that code will do is run a loop for however monsters you want spawned. So if you want 3 monsters spawned, it will run the loop 3 times before stopping execution. Next is the actual spawning code. First we're going to have it pick a random spot to spawn the monster in:

 var spawnIndex: int =  Random.Range(0, randomPositions.Length);

This basically chooses a number between 0 and the number of random positions you have. Next we'll have the code for the instantiation:

 Instantiate(monster, randomPositions[spawnIndex].position, randomPositions[spawnIndex].rotation);

And there you go. It should spawn the monsters at a random position for as many monsters as you like. Here's the complete code for reference:

 var monster: GameObject;
 
 var randomPositions: Transform[];
 var numberOfMonstersToSpawn: int = 0;
 
 function Update () {
 
    if(Input.GetButtonDown("Jump")){
       var i: int = 0;
       while(i < numberOfMonstersToSpawn){
          var spawnIndex: int = Random.Range(0, randomPositions.Length);
          Instantiate(monster, randomPositions[spawnIndex].position, randomPositions[spawnIndex].rotation); 
          i++;
       }
    }
 }

Mind you, I havent't coded in Javascript in a very very long time, and I haven't tested out any of the actual code, but it should all work. In any case, the theory is there. I wish you the best of luck!

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

12 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

Related Questions

Destroy and Spawn an Enemy 1 Answer

Enemy spawn script/player position(java/js) 1 Answer

How to spawn Prefab to a certain position. 2 Answers

choose 1 of multiple objects to spawn, respawn once its dead. 3 Answers

Spawn object in random areas 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