- Home /
An algorithm to automatically instantiate objects
I'm attempting to create an algorithm to automatically instantiate objects. I'd like the objects to have a pretty big y-index difference, to make it more difficult for players to finish the level. However, I can't seem to create such a simple thing. It could be a lack of my C# knowledge, a lack of math knowledge, or my brains are not working like they should at this moment! Hah.
Anyway, I've got the example of my script here. I hope you'll get the idea of it. If not, I'll explain it below the script.
disY = Random.Range(-6, -10);
if (disY == ldisY)
{
disY = Random.Range(-6, -10);
}
else if (disY < -8.9 && disY > -8)
{
disY = Random.Range(-9, -10);
}
else if (ldisY < -8 && disY < -8)
{
disY = Random.Range(-8, -10);
}
else if (ldisY < -8 && disY > -8)
{
disY = Random.Range(-6, -8);
}
//@ end of function
ldisY = disY;
**Note* this is in a loop which will be run 30 times. I'd like 30 objects to be instantiated.
The idea is to have a bigger distance between the y-index's. Of course a little difference between objects may occur, but I'd like to make it more of a challange. There should be a difference between 1 and 4 pixels (these values are pixels) all the time. But most of the time the y-index of every object is pretty close to each other.
So how would I make an algorithm that has a bigger difference, so the y-index will not be close to eachother. To make the question more clear, I've created a simple painting.
Thanks
Answer by flamy · Feb 07, 2014 at 07:35 AM
I see that your range is always 2, so what you can do here is after a random occurs, change the range from next one such a way that it excludes the first generated y. For example see the code below,
const float range = 2f;
const float differenceInRange =1f;
const float maxHeight = 15f;
const float minHeight= 0f;
float lastY;
void GenerateRandomHeight()
{
float approxMaxRandomNumber = lastY+differenceInRange+range;
float approxMinRandomNumber = lasty-differenceInRange-range;
int randomDirection = 0;
if(approxMaxRandomNumber > maxHeight )
{
randomDirection = -1;
}
else if(approxMinRandomNumber > minHeight )
{
randomDirection = 0;
}
else
{
randomDirection = Random.Range(0,100)%2 == 0? -1 : 1;
}
float minValue = 0;
float maxValue = 0;
if(direction == 1)
{
minValue = lastY+differenceInRange;
maxValue = lastY+differenceInRange+range;
}
else if(direction == -1)
{
minValue = lastY - differenceInRange-range;
maxValue = lastY - differenceInRange;
}
randomY = Random.Range(minValue,maxValue);
lastY = randomY;
}
With this logic the objects will have completly different values depending on the range and also the random will be maintained with a range.
PS: this is an untested code and might contain errors, this is written only for the sake of algorithm.
EDIT: you can play with the constant range or differenceInRange
more the value is on difference in range, the more random the shapes will be...
also min max heights, i have created all those constants so that you could have greater variations and play with the values.
also in start or awake see the random with some variable like time..
Random.seed = System.DateTime.Now.ToFileTime()%200;
Thank you! It's better than it used to, but right now the objects have got an order. See image:
The order has to be more random
*Image not loading? Click http://i.imgur.com/HvTVybY.png
you can play with the constant range or differenceInRange
more the value is on difference in range, the more random the shapes will be...
also $$anonymous$$ max heights, i have created all those constants so that you could have greater variations and play with the values.
also in start or awake see the random with some variable like time..
Random.seed = System.DateTime.Now.ToFileTime()%200;
Answer by fafase · Feb 07, 2014 at 07:21 AM
I am doing this manually so try and it and will see
float previousY = Random.Range(minValue,maxValue);
// Instantiate first one
// Start looping for 29 next
for (int i = 1; i < 30 ; i++)
{
// Get a random value based on the previous one
// The result will be between 1 and 4 bigger than the previous
float y = Random.Range(previousY + 1f, previousY + 4f);
// Randomly invert the sign so that it goes up and down
int sign = Random.Range(0,2);
if(sign == 0) y *= -1;
// Constrain the value within a range so that it does not get out of screen
float check = y + previousY;
if(check > maxValue) y *= -1;
if(check < minValue) y *= -1;
// Instantiate object
// Place the y value for next round
previousY = y;
}
Thank you. However, I'm not able to understand your code. Therefore I'm also not able to fix the error I receive:
PreviousOne does not exist in the context
I of course know what the error means, but I'm not able to figure out what value I should give this variable. This error also "activates" more errors
Simply because previousOne is a typo, I meant previousY
I also changed the code a little. Note that $$anonymous$$/maxValue needs to be declared as well.
The random y-index looks pretty neat, but it's completely out of the map. The y-index should stay between:
float $$anonymous$$Value = -5.1f;
float maxValue = -10.5f;
Playing with your algorithm only makes it worse though, any ideas?