创建动态GameObject并将其位置从JSON数据转换为一起

问题描述 投票:1回答:1

考虑这个数据集:

[
   {
     PartId: 0,
     Positions: [
       { x: 0, y: 0, z: 0, timeCode: 0.0000 },
       { x: 0, y: 0, z: 1, timeCode: 0.0025 },
       { x: 0, y: 0, z: 2, timeCode: 0.0050 },
       { x: 0, y: 0, z: 3, timeCode: 0.0075 },
       { x: 0, y: 0, z: 4, timeCode: 0.0100 },
       { x: 0, y: 0, z: 5, timeCode: 0.0125 },
       { x: 0, y: 0, z: 6, timeCode: 0.0150 },
       { x: 0, y: 0, z: 7, timeCode: 0.0175 },
       { x: 0, y: 0, z: 8, timeCode: 0.0200 },
     ]
   },
   {
     PartId: 1,
     Positions: [
       { x: 0, y: 0, z: 0, timeCode: 0.0000 },
       { x: 0, y: 0, z: 2, timeCode: 0.0025 },
       { x: 0, y: 0, z: 4, timeCode: 0.0050 },
       { x: 0, y: 0, z: 6, timeCode: 0.0075 },
       { x: 0, y: 0, z: 8, timeCode: 0.0100 },
       { x: 0, y: 0, z: 10, timeCode: 0.0125 },
       { x: 0, y: 0, z: 12, timeCode: 0.0150 },
       { x: 0, y: 0, z: 14, timeCode: 0.0175 },
       { x: 0, y: 0, z: 16, timeCode: 0.0200 },
     ]
   }
 ]

我能够加载和解析JSON数据,我可以为每个PartId创建一个新的GameObject。我试图找出根据Position集合同时转换每个“Part”的最佳方法。

我在场景中有一个带有附加类的空GameObject。在Start方法内部,我获取JSON数据,然后在循环中创建GameObject类,设置其初始位置,然后启动在同一个类中定义的协同程序。

主要课程:

void Start() {
    // do json stuff...
    // ........
    // then
    // for each part...

    foreach(PartGroup pg in Parts) {
        // create a new GameObject from the Part class
        Part part = gameObject.AddComponent(typeof(Part)) as Part;
        // send this part data to the new GameObject
        part.PartGroup = pg;
        // set the initial position for the part
        part.Init();

        // start a IEnumerator Coroutine in the part class
        StartCoroutine(part.PlayFrom());
    }
}

零件类:

public void Init() {
    Joint = GameObject.CreatePrimitive(PrimitiveType.Sphere);
    Joint.transform.localScale = new Vector3(jointSize, jointSize, jointSize);
    Joint.transform.position = new Vector3(PartGroup.Positions[0].X, PartGroup.Positions[0].Z, PartGroup.Positions[0].Y);
}
public IEnumerator PlayFrom(float time = 0f) {
    while (PlayBack(time)) {
        yield return null;

        time += Time.fixedDeltaTime;
    }
}
bool PlayBack(float time) {
    float sample = time / (Time.fixedDeltaTime / speed);
    int previousIndex = (int)(sample);
    int last = PartGroup.Positions.Count - 1;

    if (previousIndex < last) {
        int nextIndex = previousIndex + 1;
        float interpolation = sample - previousIndex;

        Joint.transform.position = Vector3.Lerp(
            new Vector3(PartGroup.Positions[previousIndex].X, PartGroup.Positions[previousIndex].Z, PartGroup.Positions[previousIndex].Y),
            new Vector3(PartGroup.Positions[nextIndex].X, PartGroup.Positions[nextIndex].Z, PartGroup.Positions[nextIndex].Y),
            interpolation);

        return true;
    }
    Joint.transform.position = new Vector3(PartGroup.Positions[last].X, PartGroup.Positions[last].Z, PartGroup.Positions[last].Y);

    return false;
}

这就是我目前的设置方式。它确实有效,但它不是平滑运动,有时似乎滞后或跳帧。这是实现这一目标的最佳方式,还是有更好的方法(如FixedUpdate)?我在项目设置中设置了固定时间属性以匹配数据timeCode。

任何有关此类东西的最佳实践的帮助非常感谢!

unity3d position
1个回答
2
投票

你必须使用Time.deltaTime

time += Time.deltaTime;

float sample = time / (Time.deltaTime / speed);

Coroutines与所有Update调用一起执行,因此使用fixedDeltaTime打破了框架的独立性。

或者可能使用WaitForFixedUpdateFixedUpdate一样执行它

while (PlayBack(time)) {
    yield return new WaitForFixedUpdate();

    time += Time.fixedDeltaTime;
}

也在

foreach(PartGroup pg in Parts) 
{
    // create a new GameObject from the Part class
    Part part = gameObject.AddComponent(typeof(Part)) as Part;
    // send this part data to the new GameObject
    part.PartGroup = pg;
    // set the initial position for the part
    part.Init();

    // start a IEnumerator Coroutine in the part class
    StartCoroutine(part.PlayFrom());
}

看起来你正在为列表中的每个元素添加一个组件到一个相同的GameObject ..我不知道你是否打算这样做。 AddComponent不会使用该组件创建新的GameObject,但会将该组件附加到脚本附加到的相同gameObject上。

你可能打算使用qazxsw poi

new GameObject

还有计算

Part part = new GameObject("new GameObject").AddComponent<Part>();
// make it a child of this gameObject?
part.SetParent(gameObject, false);

看起来有点奇怪..你确定它总是返回float sample = time / (Time.fixedDeltaTime / speed); int previousIndex = (int)(sample); ... float interpolation = sample - previousIndex; 0之间的值吗?

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.