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

Is it possible to create an object at a childObject's worldposition and attach the childObject to it

Discussion in 'General Discussion' started by langyinan, Aug 3, 2023.

  1. langyinan

    langyinan

    Joined:
    Aug 13, 2021
    Posts:
    1
    Is it possible to create an object at a childObject's worldposition and attach the childObject to it? Then, the childObject's local position sholud be set to 0 because it is the child of an object that is in the same place.
     
  2. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,323
  3. DragonCoder

    DragonCoder

    Joined:
    Jul 3, 2015
    Posts:
    1,463
    Not entirely sure that is what you want since your question is unclear, but prefix this with "In Unity: " and even free ChatGPT gives you code and explanation you can tinker with.
    Experiment with some cubes to understand what happens and how localPosition and regular "position" work.
     
  4. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,355
  5. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,323
    transform.position, returns world position of an object. transform.rotation, returns rotation. Those are in the world space. You can pass them into instantiate, or set them on newly constructed object, then parent. Or you can parent then zero local transforms...
     
    Antypodish likes this.
  6. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,579
    Yep. Simplest and trivial way is to attach object to parent, set local transforms to 0.
    Or optionally, set object transforms, as other object, withouth parenting.

    You can also attach to parent, set local position and detach from parent, if you are not familiar yet with transforms manipulations.
     
  7. johnkennedy169

    johnkennedy169

    Joined:
    Mar 30, 2023
    Posts:
    4
    I understand that yes, I recommend you look for tutorial videos on YouTube, you will almost always find very good information there