Alternative to "InverseTransformPoint"

Hi guys
In my code I use “InverseTransformPoint” to know the position (taking into account the rotation) within a group (planet). Everything works.

The problem is that I have created some new pieces of complex graphics on blender. When I export them this is put in the right way but their position within the planet group turns out to be zero.

So if before this worked: “Quaternion.LookRotation (transform.InverseTransformPoint (this Children.transform.position))” now it doesn’t work anymore because of course from scratch.

I attach an image with an example of my planet and a cube. in the first case, the cube has an origin that reflects the displacement from the center of the plant. in the second case the cube is always there but it has position 0 because it has already been moved to a distance.

Thanks in advance

Well your code does not make much sense to me. Assuming that this script is attached to the Planet gameobject and that “Children” is a direct child of that Planet object, that means this part:

transform.InverseTransformPoint(Children.transform.position)

Simply returns

Children.transform.localPosition

Passing a local position into LookRotation makes even less sense. LookRotation expects a direction vector as well as an optional up direction vector. If you don’t provide an up vector it will use Vector3.up.

We also don’t know how and where you use that quaternion. Do you use it as worldspace rotation for the child or as local space rotation?

Math issues aside, it sounds like you’ve re-exported your objects with their pivot points in different, less useful positions.

If you need to know the direction between objects then you’re going to have to export them with their pivot points in useful positions, such as the center of each object. There are alternative ways you could do this, but they all involve workarounds which are hacky at best.

Separately, take note of what @Bunny83 is saying about the math. Spatial transformations are tricky at first, but once you’ve got them figured out they’ll save you a lot of headaches.

Thank you guys

I’m sorry if I didn’t make myself clear. @Bunny83 actually you’re right I could also use

Children.transform.localPosition

but I’m quite new of Unity. In any case this is not good for my problem.

@angrypenguin I can’t export otherwise because I have many pieces not just a cube. each of these pieces has a different anchor.

So if I understand correctly, my problem is that my blue cubes have an anchor of 0,0,0 despite having a distinct position with respect to the origin of the group that contains them (planet).

What I was thinking of doing is: convert position and rotation of planet and children into global position, perform calculations to obtain the position and rotation of children with respect to planet. Report in coordinates equivalent to those you would get with a “.localPosition”. I don’t know how to do this and I don’t know if it can work, what do you think? Did I explain myself better? Thank you all

That’s just going the long way about getting their “local position” and “local rotation”.

If I understand correctly, you have models of things like planets where the pivot point is not in the center of the planet? Is that the case?

If so, and you are unable to re-export them with corrected pivot points, there’s another approach. You can create a new GameObject, position it at the center of each of your different models, and then make the model a child of that GameObject. This is less efficient than just fixing the models because it’s making the computer store more data and do more math to get the same results, but if you’ve no other option then that’s at least a way forward. Once in place, your scripts can then work with the position and rotation of that parent object rather than with the model GameObjects directly.

I had thought of something similar but it is really long to do and also as you said it would be a useless consumption. Isn’t there a better solution?

The “better” solution is to make sure your pivot points are in appropriate places in the model. I’m not a 3D modeller, but you may find that there’s a way to set these in bulk in the modelling package you’re using?

Or are these assets you’ve purchased from someone? That makes it a real pain.

A little buying and a little I’m doing it alone but I’m not a 3D modeler either. And so this is very problematic