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!
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
}
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.
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.