Hello, I have achieved dynamic creation of objects to the Earth’s surface by converting Unity coordinates through latitude and longitude. However, I need to connect two objects through lines, which requires me to obtain the position of the two objects. However, the position value I see through the “Inspector” panel is different from the value I print out through Debug. Why is this?
Why are you adding a CesiumOriginShift component? That’s almost certainly the wrong thing to do, as it really only ever makes sense to have a single GameObject with a CesiumOriginShift in your scene. Perhaps you meant to add a CesiumGlobeAnchor component instead?
But if I add the CesiumGlobeAnchor component, there will be issues with its coordinate system, so I am not sure which of the CesiumGlobeAnchor and CesiumOriginShift really contribute to my dynamic creation of objects on Earth
What sort of issues do you mean?
Floating point issue, I cannot locate the object I dynamically created
If you want help, you’re going to have to walk me through it.
CesiumOriginShift, when attached to a single object (your player/camera typically) keeps the Unity origin near that object, so coordinate values are small and precision is preserved. I’m not arguing against using CesiumOriginShift if you need it.
However, putting a CesiumOriginShift on multiple game objects is nonsensical. There’s only one Unity coordinate system, so it can’t be placed in multiple places at once.
The Position value in the inspector panel of the image above has always been a problem that troubles me
Well, I added CesiumOriginShift to the camera, but when I dynamically create objects based on longitude and latitude and add CesiumGlobeAnchor, the object often does not appear on Earth, so I don’t know where the problem is. In my first screenshot, there is my code for converting longitude and latitude to Unity coordinates.
Please walk me through it step by step. How is your scene set up? What code are you using? (hopefully you’ve now removed the CesiumOriginShift from the code you originally shared above?) When is that code executed? What is happening? What do you expect to happen? I unfortunately can’t read your mind to debug your program, so you have to give me all the information. Statements like “the object often does not appear on Earth” won’t help me help you, because I don’t know exactly what you’re doing when they don’t appear, and I don’t even know what you mean. Do they appear somewhere else other than on Earth?
好的,首先感谢你不厌其烦的帮我解决问题,其次是我场景配置
我现在的需求就是通过多个经纬度使用LineRender渲染把它们连接起来,但是首当其冲的一个问题就是经纬度转换成Unity坐标,我通过官方教程和论坛,认为找到正确转换坐标的方法,但是程序运行的时候转换的Unity.Position的值会提示“transform.localPosition assign attempt for ‘New Game Object’ is not valid. Input localPosition is { NaN, NaN, NaN }.”,我在Game窗口看不到我生成的物体且Hierarchy面板双击生成的物体也定位不到目标
Code for converting longitude and latitude to Unity coordinates:
public void CesiumPosForUnityPos(double3 db)
{
var ecef = CesiumWgs84Ellipsoid.LongitudeLatitudeHeightToEarthCenteredEarthFixed(db);
double3 unity = cesiumGeoreference.TransformEarthCenteredEarthFixedPositionToUnity(ecef);
GameObject obj2 = new GameObject();
obj2.gameObject.transform.SetParent(cesiumGeoreference.transform);
obj2.transform.position = new Vector3((float)unity.x, (float)unity.y, (float)unity.z);
obj2.AddComponent().longitudeLatitudeHeight = ecef;
Debug.Log(obj2.transform.position);
}
Ok, several things here.
First, your CesiumGeoreference’s Height
is huge. Do you really intend for the origin of the Unity coordinate system to be 6.5 million meters above the surface of the globe?
Second, your CesiumPosForUnityPos
is broken. This line makes no sense at all:
obj2.AddComponent().longitudeLatitudeHeight = ecef;
ECEF coordinates and longitude/latitude/height are not the same thing. This might be closer:
obj2.AddComponent().longitudeLatitudeHeight = db;
You don’t even need most of the rest of that code.
But make sure that db.x
is the longitude and db.y
is the latitude. It’s a common mistake to reverse these, and it will lead to completely wrong results.
You didn’t include your code in a code block, so I can’t tell what sort of component that AddComponent is adding because the generic argument was treated as HTML. Presumably it’s a CesiumGlobeAnchor?
Thank you again for helping me solve this problem. The method you told me was correct and I have used it before. Secondly, it made me realize that my main problem was that I once thought that the “warning” of the Inspector panel’s Pos value was an error, which caused me to keep modifying the code. This is incorrect. Thank you again