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 Robomaster · Sep 03, 2012 at 02:20 PM · randomtilesspawner

Semi Random Monster Spawner

Hello, in my game there are some tiles that will be monster tiles. What i want these tiles to do is when the player enters its trigger effect a semi random monster appears. I can code the trigger effect but how would i code, for example 25% chance of demon, 50% chance of goblin, 25% chance of Ogre. Thanks in advance!

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
0
Best Answer

Answer by Mander · Sep 03, 2012 at 02:38 PM

 number = Random.Range(1.0, 4.0);
 
 if(number == 1)
 {
 //spawn enemy 1
 }else if(number >1 && number <3)
 {
 //spawn enemy 2
 }else if(number == 4)
 {
 //spawn enemy 3
 }
Comment
Add comment · Show 20 · 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 Robomaster · Sep 03, 2012 at 02:40 PM 0
Share

Thanks so much is that in Javascript or C#

avatar image AlucardJay · Sep 03, 2012 at 02:43 PM 0
Share

or ...

 switch ( Random.Range(0, 4) )
 {
     case 0 :
         //spawn enemy 1
     break;
     case 1 :
         //spawn enemy 2
     break;
     case 2 :
         //spawn enemy 2
     break;
     case 3 :
         //spawn enemy 3
     break;
 }

  • note the API for Random (especially if you only want integers) : http://docs.unity3d.com/Documentation/ScriptReference/Random.Range.html

static function Range ($$anonymous$$ : int, max : int) : int

Returns a random integer number between $$anonymous$$ [inclusive] and max [exclusive] (Read Only).

this means for an int between 1 and 4, use Random.Range(1, 5) (1 is inclusive, 5 is exclusive, so only numbers between 1 and 4 are returned.

=]

edit using switch-case :

 case 0 :
     // INSERT YOUR CODE HERE =]
 break;

e.g.

 case 0 :
     Debug.Log("I'm condition 0 !");
     someVar += someNumber;
     someBoolean = false;
     Instantiate( myObj1, transform.position, Quaternion.identity );
 break;

 case 1 :
     Debug.Log("I'm condition 1 !");
     someVar -= someNumber;
     someBoolean = true;
     Instantiate( myObj2, transform.position, Quaternion.identity );
 break;
avatar image Mander · Sep 03, 2012 at 02:46 PM 0
Share

@Robomaster could be either js or c# the logic is the same for both. u only would need to declare the number var

avatar image Robomaster · Sep 03, 2012 at 02:51 PM 0
Share

Thanks for your help but i have a question will the spawn enemy _ break; do anything becuase i thought if a sentence had a // before it, it became a comment?

avatar image save · Sep 03, 2012 at 02:53 PM 0
Share

A convenient approach is also http://docs.unity3d.com/Documentation/$$anonymous$$anual/RandomNumbers.html for more advanced function of probability.

Show more comments
avatar image
0

Answer by DESTRUKTORR · Sep 03, 2012 at 02:39 PM

Either you'll have to use bounding boxes set as triggers, or just programmatically detect when the player enters a new tile. I'm going to assume that since you said it would be tile based that the player's movement will be restricted to these tiles, in which case the latter would likely be a pretty viable option. As for the "semi-randomness," take a look at the Random class, built into Unity.

Common practice for "semi-random" events in games is to use previously set arrays or lists, and then randomly choose from them, based on their size.

So, basically, if you want those percentages, simply have a random number between 0 and 3 (4 numbers) picked, and, say, if it's 0, it returns a demon, if 1, returns an ogre, and if greater than 1, returns a goblin. You can also use ranges of numbers to do this, such as if it's greater than 0 and less than 3, return a goblin, but in this case, it would be simpler, and more readable, to do it the first way.

If you want me to post code, I need to see some of yours, first. I'm not going to do your work entirely for you, but I will help you along.

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 jmfletc · Sep 12, 2012 at 10:49 PM

is it possible to import a random number generator and declare a variable of "enemy" and have it spawn every so often after each conditional statement(In java) that would make more sense to me.

Comment
Add comment · Show 1 · 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 AlucardJay · Sep 13, 2012 at 06:42 AM 0
Share

I'm not sure of your point here. Is this an answer, or asking a question? This question was answered 9 days ago, the OP codes in C#, and you have just re-stated the answer. Both the IF way and the Switch way generate a random number, then based on that outcome a monster is chosen. What is your point?

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

Spawning an object at a random time | C# 2 Answers

Adding a timer to spawner 2 Answers

Random Spawn, Random Prefab 2 Answers

Random Modular Hex Board for board game 1 Answer

Enemy Spawner based on enemies values ! 0 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