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

Resolved Doubt with code - Linear Distribution in Script

Discussion in 'Scripting' started by VengarlofForossa, Sep 29, 2023.

  1. VengarlofForossa

    VengarlofForossa

    Joined:
    Jan 1, 2023
    Posts:
    18
    Hello everyone!

    I have a doubt with the following question

    In Unity, you can select several objects at once, and in the Inspector you can set L(8, -8) to the X position. This makes the objects arrange themselves perfectly between those two points.

    Is there any way to do this in code?

    For example I have four objects that have the same parent, could I select them and perform this action by code?
    I ask this in case there is a simple way to do it.

    Thank you very much for your time
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,379
    I assume you're asking if there is a way via script that you can just call a method that you pass the string "L(8, -8)" to and get the same result?

    Considering that I only just learned today that this even is a thing in Unity. Nope... I don't know of a way to do that via code. Maybe there is? But I wouldn't be surprised if it's not available.

    ...

    If you want a way to just position them along a line... you could just do this:

    Code (csharp):
    1. static IEnumerable<float> LinearDistribution(float min, float max, int count)
    2. {
    3.     if (count <= 1)
    4.     {
    5.         yield return return Mathf.Lerp(min, max, 0.5f);
    6.         yield break;
    7.     }
    8.  
    9.     float delta = 1f / (count - 1);
    10.     for (int i = 0; i < count; i++)
    11.     {
    12.         yield return Mathf.Lerp(min, max, delta * (count - 1));
    13.     }
    14. }
    15.  
    16. var objs = *get some transforms*;
    17. int i = 0;
    18. foreach (var x in LinearDistribution(-8f, 8f, objs.Length))
    19. {
    20.     var pos = objs[i].position;
    21.     pos.x = x;
    22.     objs[i].position = pos;
    23.     i++;
    24. }
     
    VengarlofForossa likes this.
  3. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,917
    To put it another way, you can write a script to figure out N equally-spaced positions between -8 and 8 (or any other values), but your code has to do all of the math itself.

    For example, -8 to 8 is a range of 16. The script needs to have something like
    range=last-first;
    to get that. To get the spacing, you have to realize that a point is on each end, so if you have 5 points there are 3 in the middle, which divides the range into 4 parts, so the size of the gap between points is
    range/(pointCount-1)
    . That has to be in the script. Then they need to be placed using:
    first+gap*pointNumber
    (pointNumber goes from 0 to one less than the # of points to place). That's not difficult math -- it's 7th-grade algebra at best -- and it can be turned into a function (by you) where you give it first, last and pointCount and it spits out an array of the values. That makes it feel built-in and makes it so you can easily use it after you've forgotten exactly how the equations work.
     
    VengarlofForossa likes this.
  4. VengarlofForossa

    VengarlofForossa

    Joined:
    Jan 1, 2023
    Posts:
    18
    Thank you very much Lord and Owen. I was looking on the internet to see if there was a "native" way but nothing.

    So I have found the solution by putting into practice what you both have told me. I have a List with the objects, so I make a coroutine to which I pass the List that I go through and with mathematical calculations I place them.

    And really, thank you very much for your time.
     
    Last edited: Oct 6, 2023