Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Equation where x=0 output = 100

Discussion in 'Scripting' started by Killercarrot102, Aug 29, 2023.

  1. Killercarrot102

    Killercarrot102

    Joined:
    Jun 20, 2022
    Posts:
    10
    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:

    Code (CSharp):
    1.         for (int x = -10; x <= 10; x = x + 5)
    2.         {
    3.  
    4.            
    5.             int[] weights = { 100, 0, 0, 0 };
    6.             int weightedRandom = WeightedRandom(weights);
    7.             GameObject curentPerson = Instantiate(people[weightedRandom], gameObject.transform);
    8.             curentPerson.GetComponent<PersonMovment>().playerPos = pos;
    9.             curentPerson.GetComponent<PersonMovment>().Source = src;
    10.             curentPerson.GetComponent<PersonMovment>().sco = score;
    11.             curentPerson.transform.position = new Vector2(x, 1.1f);
    12.             for (int i = 0; i < weights.Length; i = i + 1) {
    13.                 weights.SetValue(PUT MATH HERE, i);
    14.             }
    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.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,713
    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.
     
    CodeRonnie likes this.
  3. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    :eek: SWEET MOTHERBOARD OF SILCON!!!
    judging by my upside-down calculations, this only iterates 4 times? So a simple
    i < 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:
    Code (CSharp):
    1. PersonMovment personScript = curentPerson.GetComponent<PersonMovment>();
    2. personScript.playerPos = pos;
    3. personScript.Source = src;
    4. personScript.sco = score;
    I would like to give more insight, but the
    x = -10; x = x + 5;
    thing is confusing me. :confused:
     
    PraetorBlue and DragonCoder like this.
  4. Killercarrot102

    Killercarrot102

    Joined:
    Jun 20, 2022
    Posts:
    10
    [QUOTE="wideeyenow_unity,

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

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

    Killercarrot102

    Joined:
    Jun 20, 2022
    Posts:
    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: https://www.desmos.com/calculator
     
  6. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    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?!".
     
  7. sngdan

    sngdan

    Joined:
    Feb 7, 2014
    Posts:
    1,131
    y = 100 - x
     
  8. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    Sure, this notation can look weird
    Code (csharp):
    1. for(int x = -10; x <= 10; x = x + 5) {
    2.   ...
    3. }
    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
    Code (csharp):
    1. int x = -10;
    2. while(x <= 10) {
    3.  …
    4.  x += 5;
    5. }
    Or
    do
    loop
    Code (csharp):
    1. int x = -10;
    2. do {
    3.   ...
    4.   x += 5;
    5. } while(x <= 10);
    All of these will have the same number of iterations, where x =
    Code (csharp):
    1. -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.

    Code (csharp):
    1. for(int i = 0; i < 5; i++) {
    2.   int x = 5 * i - 10; // -10, -5, 0, 5, 10
    3. }
    It becomes a little more problematic if you don't know the count of iterations in advance. In that case:
    Code (csharp):
    1. int start = -10;
    2. int end = 10;
    3. int step = 5;
    4. int count = (end - start) / step + 1;
    5.  
    6. for(int i = 0; i < count; i++) {
    7.   int x = start + step * i; // -10, -5, 0, 5, 10
    8. }
    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.

    Code (csharp):
    1. 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

    Code (csharp):
    1. var start = -10f;
    2. var end = 10f;
    3. var step = 1f / 4f;
    4.  
    5. for(float t = 0f; t <= 1f; t += step) {
    6.   int x = (int)MathF.Floor(lerp(start, end, t)); // -10, -5, 0, 5, 10
    7. }
    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.
    Code (csharp):
    1. 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
    Code (csharp):
    1. static float invLerp(float a, float b, float b) => substNaN((v - a) / (b - a), 1f);
    2. static float substNaN(float n, float s) => float.IsNaN(n)? s : n;
    Now you can define remapping as
    Code (csharp):
    1. static public float remap(float i1, float i2, float o1, float o2, float v)
    2.   => 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
    Code (csharp):
    1. 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.
    Code (csharp):
    1. var t = invLerp(minPower, maxPower, someGuy);
    And then make an AnimationCurve as suggested by Kurt-Dekker, to which you can plug your t
    Code (csharp):
    1. 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.
     
    wideeyenow_unity likes this.
  9. dlorre

    dlorre

    Joined:
    Apr 12, 2020
    Posts:
    700
    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: https://en.wikipedia.org/wiki/Lagrange_polynomial

    It looks scary but it's not that bad.
     
  10. Killercarrot102

    Killercarrot102

    Joined:
    Jun 20, 2022
    Posts:
    10
    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.