Child 在运行时忽略对 Parent 变换位置的更改。怎么修?
Child ignores changes to Parent's transform position during runtime. How to fix?
我将球体实例化为 GameObject 的子对象。我希望这些球体跟随游戏对象的位置。
问题是,这些球体顽固地停留在它们生成的地方,并且拒绝在运行时检查器中的任何变换位置发生变化时改变它们的位置。
spheres = new GameObject[mesh.vertexCount]; //create gameobject array, size of mesh vertex count
for (int i = 0; i < mesh.vertexCount; i++)
{
matrices1pos[i] = matrices1[i].position;
spheres[i] = Instantiate(spherePrefab, matrices1[i].position, Quaternion.identity);
spheres[i].transform.SetParent(GO2.transform);
}
我什至尝试添加代码:
spheres[i].transform.localPosition = Vector3.zero;
但这也不会使球体响应父级的位置变化。
顺便说一句,Spheres 的生成位置是脚本运行的 GameObject(称为 MeshChanger
)的位置。在运行时更改 MeshChanger
转换的位置也没有效果。
编辑:已解决!请参阅下面的答案
我明白了。我需要使用 localPosition。 =]
spheres[i].transform.SetParent(GO2.transform);
spheres[i].transform.localPosition = Vector3.Lerp(matrices1pos[i], matrices2[i].position, t);
我将球体实例化为 GameObject 的子对象。我希望这些球体跟随游戏对象的位置。
问题是,这些球体顽固地停留在它们生成的地方,并且拒绝在运行时检查器中的任何变换位置发生变化时改变它们的位置。
spheres = new GameObject[mesh.vertexCount]; //create gameobject array, size of mesh vertex count
for (int i = 0; i < mesh.vertexCount; i++)
{
matrices1pos[i] = matrices1[i].position;
spheres[i] = Instantiate(spherePrefab, matrices1[i].position, Quaternion.identity);
spheres[i].transform.SetParent(GO2.transform);
}
我什至尝试添加代码:
spheres[i].transform.localPosition = Vector3.zero;
但这也不会使球体响应父级的位置变化。
顺便说一句,Spheres 的生成位置是脚本运行的 GameObject(称为 MeshChanger
)的位置。在运行时更改 MeshChanger
转换的位置也没有效果。
编辑:已解决!请参阅下面的答案
我明白了。我需要使用 localPosition。 =]
spheres[i].transform.SetParent(GO2.transform);
spheres[i].transform.localPosition = Vector3.Lerp(matrices1pos[i], matrices2[i].position, t);