- Home /
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!
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
}
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;
@Robomaster could be either js or c# the logic is the same for both. u only would need to declare the number var
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?
A convenient approach is also http://docs.unity3d.com/Documentation/$$anonymous$$anual/RandomNumbers.html for more advanced function of probability.
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.
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.
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
![](https://koobas.hobune.stream/wayback/20220613080553im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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