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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

What is possible to write in Transform in the editor

Discussion in 'General Discussion' started by Zeuch, Feb 6, 2023.

  1. Zeuch

    Zeuch

    Joined:
    Aug 26, 2017
    Posts:
    3
    I have been searching a lot but could not find it. I have seen a tips regarding the transform in the editor. Say that you have 3 gameobjects, and you want to place with a certain space between them. I remember seeing this somewhere. but can not find it or figure out what to write.
     
  2. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,337
  3. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,184
    If you only have a vague idea of what you want to accomplish or don't know the appropriate keywords to search for a solution on your own you're best off just asking ChatGPT.

    I have been searching a lot but could not find it. I have seen a tips regarding the transform in the editor. Say that you have 3 gameobjects, and you want to place with a certain space between them. I remember seeing this somewhere. but can not find it or figure out what to write. Can you show a complete example?

    Here's a complete example in C#, which you can use to place 3 game objects with a certain space between them in Unity:

    Code (csharp):
    1. using UnityEngine;
    2.  
    3. public class PlaceGameObjects : MonoBehaviour
    4. {
    5.    public GameObject object1, object2, object3;
    6.    public float spaceBetweenObjects = 2f;
    7.  
    8.    void Start()
    9.    {
    10.        Vector3 position1 = object1.transform.position;
    11.        Vector3 position2 = position1 + new Vector3(spaceBetweenObjects, 0f, 0f);
    12.        Vector3 position3 = position2 + new Vector3(spaceBetweenObjects, 0f, 0f);
    13.  
    14.        object2.transform.position = position2;
    15.        object3.transform.position = position3;
    16.    }
    17. }
    In this example, object1, object2, and object3 are the 3 game objects that you want to place with a certain space between them. spaceBetweenObjects is the distance between each game object, which is set to 2f in this example.

    In the Start method, the positions of the 3 game objects are calculated based on object1's position and spaceBetweenObjects. Finally, the positions are set to object2 and object3 using the transform.position property.
     
    DragonCoder and SunnySunshine like this.
  4. Zeuch

    Zeuch

    Joined:
    Aug 26, 2017
    Posts:
    3
    I remember it like this. you had 2 gameobjects that you in the editor, want to place like 10 units from eachother. then you wrote something like random(10) or something. in the editor itself. So no code in c# or anything. and I checked the manual.

    So 2 objects(or more), write in the positionfield (the code that i need) like 10x. and then the units will be placed like y coordinat 0, and the next object is 10 on the y.(and if you would have more objects it would be placed at 20,30 etc)

    But I guess its not possible and I remember it wrong. :(
     
  5. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,337
    This may be a feature from another modeling software.
     
  6. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
    I also remember once reading about this in a CatLikeCoding-tutorial.


    Found this:
    Linear or random distribution of GameObjects in the Inspector (manuel-rauber.com)

    Select a bunch of objects and then type in: L(0, 100). It will now distribute all selected items evenly between 0 and 100. If you use R(0, 100) it instead, it will randomly distribute all selected items between 0 and 100.


    Edit: Also available here, under "Numeric field expressions": Unity - Manual: Editing properties (unity3d.com)
     
    Zeuch, neginfinity and DragonCoder like this.
  7. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,337
    As usual, unity does amazing job demonstrating built in functionality. I'm not sure what I'd need to do to stumble upon this.
     
  8. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
    To be fair, this is quite a niche functionality, that you'll hardly ever use. Usually you'd do this in scripts.
     
  9. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,337
    Not really, this is extremely useful for modular building, such as column placement/etc.
     
  10. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
    During modular building you'll usually use Grid Snapping: Unity - Manual: Grid snapping (unity3d.com)
    It's only useful when you want to specifically position a line of objects at once.
     
  11. Zeuch

    Zeuch

    Joined:
    Aug 26, 2017
    Posts:
    3
    Thaaaanks!! this is what I am looking for!
     
  12. DragonCoder

    DragonCoder

    Joined:
    Jul 3, 2015
    Posts:
    1,492
    Well, it is in the docs after all. Including visualization etc. Not necessarily their fault people will likely not read that whole section because besides this, everything else there is trivial. It does fit in that section.
    Didn't know about this either :D

    Maybe they should create a YT-Short on their channel to show this. Would certainly be cool.