Equation where x=0 output = 100

I am making a spawning script for enemies in my game and i want the least powerful people to be less common to spawn when the abs y value gets bigger and when x = 0 the output if the equation = 100 meaning 100% chance that a weak enemy will spawn.

i have some code here but not an equation:

        for (int x = -10; x <= 10; x = x + 5)
        {

            
            int[] weights = { 100, 0, 0, 0 };
            int weightedRandom = WeightedRandom(weights);
            GameObject curentPerson = Instantiate(people[weightedRandom], gameObject.transform);
            curentPerson.GetComponent<PersonMovment>().playerPos = pos;
            curentPerson.GetComponent<PersonMovment>().Source = src;
            curentPerson.GetComponent<PersonMovment>().sco = score;
            curentPerson.transform.position = new Vector2(x, 1.1f);
            for (int i = 0; i < weights.Length; i = i + 1) {
                weights.SetValue(PUT MATH HERE, i);
            }

the second for loop loops over every index in weights and sets it to the output of the equation.

it would also be helpful if the equation had a place to put a value where it outputs 100 so instead of x=0 outputs 100, x=some int outputs 100.

Rather than making a hairy equation, you could use an AnimationCurve property to set it graphically.

Otherwise, if you insist on writing a function, I would start by making a simple graph of what you want for this function.

1 Like

:hushed: SWEET MOTHERBOARD OF SILCON!!!
judging by my upside-down calculations, this only iterates 4 times? So a simplei < 4; is all you need, you only need to modify what “i” translates to later in the loop.

Also, you can make this simpler and more performant by just saying:

PersonMovment personScript = curentPerson.GetComponent<PersonMovment>();
personScript.playerPos = pos;
personScript.Source = src;
personScript.sco = score;

I would like to give more insight, but the x = -10; x = x + 5; thing is confusing me. :face_with_spiral_eyes:

2 Likes

[QUOTE="wideeyenow_unity,

I would like to give more insight, but the x = -10; x = x + 5; thing is confusing me. :face_with_spiral_eyes:[/QUOTE]

the loop goes for evry fifth x value in my game world and starts at x -10 and goes to 10

I made a desmos thing have no idea how to put it into code

the desmos thing: \left(y-100\right)<x\cdot w\left{x\cdot w<0-\left(y-100\right)\right}\ \left{y>0\right}

desmos: Desmos | Graphing Calculator

uh huh… It’s purely beating around the bush, just use how ever many iterations you want(need), then calculate what “i” is in representation of later in the loop(or in this case “j” since you have a second for lop with “i”).

As I’m sure when you come back to this method later in life, you’ll be like “what the… who wrote that, and why?!”.

y = 100 - x

Sure, this notation can look weird

for(int x = -10; x <= 10; x = x + 5) {
  ...
}

but it’s not unheard of
I would only change x = x + 5 to x += 5 because it does make it easier to read.

Another way to do this is perhaps by doing a while loop

int x = -10;
while(x <= 10) {
 …
 x += 5;
}

Or do loop

int x = -10;
do {
  ...
  x += 5;
} while(x <= 10);

All of these will have the same number of iterations, where x =

-10, -5, 0, 5, 10

Now depending on what you’re trying to achieve, it might be smarter to start off from the premise of having a known count of iterations. This changes the code into counter/transformation recipe, where you can keep your counter fairly simple to read.

for(int i = 0; i < 5; i++) {
  int x = 5 * i - 10; // -10, -5, 0, 5, 10
}

It becomes a little more problematic if you don’t know the count of iterations in advance. In that case:

int start = -10;
int end = 10;
int step = 5;
int count = (end - start) / step + 1;

for(int i = 0; i < count; i++) {
  int x = start + step * i; // -10, -5, 0, 5, 10
}

But let me show you another trick to help you untangle from these things altogether.
What you’re doing here is simply a uniform spread of points along some line.
So the transformation shown above is known as a linear transformation.

This means you can easily apply linear interpolation (aka the lerp), and in fact an advanced form of it, known as linear remapping.

Here’s what linear interpolation does: Given some start and end, compute any point in between using some measure of weight between them, think of it as a percentage. This, in general, works the best with floating points, but it can be adjusted to work with the integers as well. It’s for the best to apply this kind of integer rounding right at the end, however.

static float lerp(float a, float b, float t) => a * (1f - t) + b * t;

Where t is known as the interpolant. Now you can do

var start = -10f;
var end = 10f;
var step = 1f / 4f;

for(float t = 0f; t <= 1f; t += step) {
  int x = (int)MathF.Floor(lerp(start, end, t)); // -10, -5, 0, 5, 10
}

But here’s what linear remapping is all about.
First you do an inverse of the lerp, where you pass in the actual value, and get back the interpolant t.

static float invLerp(float a, float b, float v) => (v - a) / (b - a);

This can backfire when a and b are the same value, so I like to do this to define the infinitesimal range between them

static float invLerp(float a, float b, float b) => substNaN((v - a) / (b - a), 1f);
static float substNaN(float n, float s) => float.IsNaN(n)? s : n;

Now you can define remapping as

static public float remap(float i1, float i2, float o1, float o2, float v)
  => lerp(o1, o2, invLerp(i1, i2, v));

Which doesn’t look terribly complicated in itself right?
Now you have a universal linear transformator that can easily transform a value from one line to another.

If there is a linear relationship between the two, this is as simple as

var x = remap(minPower, maxPower, maxFrequency, minFrequency, someGuy);

Notice that the frequency min and max are swapped, meaning that if someGuy is closer to minPower, the outcome will be closer to maxFrequency.

And if the relationship isn’t linear, at least make sure you get the interpolant first.

var t = invLerp(minPower, maxPower, someGuy);

And then make an AnimationCurve as suggested by Kurt-Dekker, to which you can plug your t

var frequency = myCurve.Evaluate(t);

I don’t know if you can see what I see, but you should be free from the perils of living among the filthy numbers, as if they carry meanings by themselves.

1 Like

Why don’t you give example of the weight values for each value of x? Then it’s easy to give you the formula. The only thing is that I don’t remember if it’s Laplace or Lagrange who gave the method. IIRC this can be turned into a polynom.

There it is: Lagrange polynomial - Wikipedia

It looks scary but it’s not that bad.

Guys i figured it out! the equation is \left(0-\operatorname{abs}\left(\frac{x}{w}-p\right)+100\ \right) in desmos. w is used to control width and p is the x where y = 100.