1
\$\begingroup\$

I'm sure I am misunderstanding something about Unity vectors or something here. I am brand, brand new to Unity so this is part of the problem.

I am trying to make a simple atomic model and I am trying to model valence electrons.

I've got a game object with two children - the symbol (which is working perfectly), and a child electron.

If an atom has more than 1 valence electrons, I add those as additional child objects.

All of this is working just fine - except for one part. I have a default position for the original child valence electron, but when I try to add additional children and set their positions, they're not anywhere near where they should be.

For example:

My Model:

My Model

My base electron:

My base electron

My additional electrons look like this:

My additional electrons look like this

And here is my output - obviously not right:

And here is my output - obviously not right

So here's my function to add additional electrons:

  void addElectrons(Rigidbody2D atom)
  {
    AtomData.AtomData atomData = atom.GetComponent<Atom>().atomData;

    if (atomData.ValElec > 1)
    {
      for (int i = 1; i < atomData.ValElec; i++)
      {
        GameObject electron = Instantiate(electron_obj, new Vector2(0.2f, 0), Quaternion.identity) as GameObject;
        electron.name = "Electron " + (i + 1).ToString();
        //electron.transform.localPosition = new Vector3(.02f, 0, 0);
        electron.transform.localScale = new Vector3(.05f, .05f, 0);
        electron.transform.parent = atom.transform;
      }
    }
  }

I've tried Vector2 (currently shown above), a new Vector3 in transform.localPosition (shown and commented out), as well as in transform.position (not shown, but similar to localPosition).

What exactly am I doing wrong here? I am strongly guessing that I am misunderstanding the vector math or something here.

\$\endgroup\$
  • \$\begingroup\$ Did you try setting your local position after assigning your parent? (And/or setting parent with worldPositionStays == false?) \$\endgroup\$ – DMGregory Feb 10 at 20:02
  • \$\begingroup\$ Doh. That was it. I didn't even think about that - I figured it was going to correctly calculate position all at runtime. \$\endgroup\$ – Andrew Alexander Feb 10 at 20:04
  • \$\begingroup\$ Glad you solved it! Feel free to write up your solution as an answer then. :) \$\endgroup\$ – DMGregory Feb 10 at 20:04
0
\$\begingroup\$

So the answer was that I needed to set the localPosition after setting the parent.

I thought everything was correctly calculated at runtime (taking parental relationships into account), but apparently if you set the localPosition before setting the parent, it does NOT take the future parent into account at all, and uses another reference point (I assume the origin?) to generate the position.

\$\endgroup\$

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.