- Home /
How to fix child gameobject position in relation to sphere parent transform.
Hi everyone, this is my first question here. I been trying to position some random cubes at runtime( st start function) via script over the surface of a sphere. I have an script that changes from Cartesian coords to latitude and longitude and then fix the upper vector to face out relative to the sphere. The problem is that not all my cubes appear on top of the surface, some of them just get under the surface of the sphere or embedded half way on it. The thing is that the squares at some point are going to represent buildings and the sphere a planet. If I take (in my case) from google map the coords of Google HQ and apply it to a square in my scene and also add an Planet Earth texture to my sphere, the coords match, but if I start adding cubes using known coords from other buildings around the world (for testing) sometimes the cubes get stock between the sphere surface.
The code that I been using for the coords transformation is from: https://unitycoder.com/blog/2016/03/01/latitude-longitude-position-on-3d-sphere-v2/
The hierarchy of my game objects is as follow:
ParentGO -> Sphere -> cube
So, I'm not sure if I might need to give it some sort of offset to the cubes, but that haven't work for my at the moment. Does anyone has a suggestion or see something that i might be doing wrong or missing?
I take it that you're aligning your cubes by rotating them, so one possibility is that your cubes origin isn't at the center of the cubes. If that's the case, if you spawn lots of cubes, the ones above the sphere surface should be at one hemisphere, while the ones below the sphere surface would be at the opposite hemisphere. Is that so? Then make sure their origin is at their center before aligning them.
Answer by Jyito · Nov 27, 2018 at 01:24 AM
Well I actually do is:
instantiate(cube) where the cube prefab is at 0,0,0
Assign cube as a child to sphere
Get some realworld coordinates from knwon buildings from a csv
Send the CSV coords to the PolarToCartesian(), which return x,y,z
Assign the X,Y,Z to the cube.
Finally fix the upward vector using `Quaternion.FromToRotation(up_Vector, DiffBetweenCube and Sphere) * cube.transform.rotation;
Apply that Rotm to the cube.
So, what you actually mean, is that I need to do the rotation before assigning the position in the above steps?
Honestly I can't tell if the rotation is being applied correctly from this line Quaternion.FromToRotation(up_Vector, DiffBetweenCube and Sphere) * cube.transform.rotation;
. But what I'm saying is, assu$$anonymous$$g it is, maybe your cube prefabs origin(pivot point) isn't sitting at the center of your cubes, and therefore rotating them is causing them to sit at different heights (and slightly misaligned) in your sphere surface. It's just a possibility for you to consider. Not saying this is what's actually happening.
Your answer
![](https://koobas.hobune.stream/wayback/20220612182813im_/https://answers.unity.com/themes/thub/images/avi.jpg)