Transform (from Unity to C++)

Hi All

My task is to port some Unity open-source code to C++, but I never writting on C# nor scripts. So my naive questions are:

  1. Are Transform(s) “shared” when one is assigned to another?

  2. How are Transforms synchronized? For example a line of code does modify position and/or rotation properties. Are other data (matrices for example) adjusted automatically? Are child objects’ transforms also adjusted auto?

  3. There is a some “GameObject” that has a “Controller”. So which/whose transform is used? How are these transforms related?

Thank you

In Unity, “GameObjects” are a special object that come predefined from Unity. Most of your scripts you attach to game objects, and in unity parlance, these are ‘components’. A Transform is a special kind of component, and every GameObject has exactly one Transform. Although it may look like components also have a Transform, this is not true; if you access a Transform in unity (without prefix), special code inside Unity will rewrite that statement to access the parenting game object’s transform, i.e.

transform.position

becomes

this.gameObject.transform.position

So:

  1. All components of an object ‘share’ the parenting game object’s transform in that they all point to the same Transform object.

  2. They aren’t synchorinized between components, they all access the same transform (the transform that belongs to the game object they are attached to). Child Transforms (i.e. Transforms that belong to game objects that have a game object as parent) do not need to be updated when their parent’s Transform changes, because they are always local, and you can get the global representation by multiplying it (the child transform) with all parent transforms.

  3. the “Controller” is a script (component) that attaches to a game object and therefore accesses that game object’s transform.

Hello csofranz

Thx for your help, clear with sharing

Example

  • first a script changes transform.rotation
  • then the script calls transform.TransformVector

As I understood, in ported C++ code, after rotation changed, I should recompute transform.localToWorldMatrix (that’s used in my TransformVector implementation) and all child transforms. Please correct if I’m wrong

Thank you

In Unity that’s not required, Unity updates toWorld for you whenever you change a Transform and you can . Furthermore, when you access

Transform.position you are always receiving the world coordinates (to-world is automaticallly applied)
Transform.localposition returns the local coordinates
Transform.forward is in world space
Transform.Up is in world space
etc.

Rotation works similar.

You can use transform.TransformPoint at any time to transform a local point to a world point.

In short: when you use the Transform methods, Unity will make sure that all to-world and reverse transforms are updated automatically. I now realize that my phrasing above was very misleading, so to be more precise - when you change the Transform in Unity through Unity-provided methods, Unity will take care of re-calculating all dependent to-world and inverse transforms. When you change the Transform of a parent, you do NOT have to update the child’s Transforms yourself.

Clear, thank you