【问题标题】:Unity: transform child's position and leave parent where it is统一:转换孩子的位置并将父母留在原处
【发布时间】:2018-08-13 18:56:25
【问题描述】:

我想将 itemChild 从父级移开,但父级总是与子级一起移动到新位置。为什么父母要跟随孩子的立场?我怎样才能移动孩子并将父母留在原地。 我的代码:

GameObject go = new GameObject("go");
    go.transform.parent = GameObject.FindGameObjectWithTag("Canvas").transform;
    itemChild.transform.SetParent(go.transform);
    itemChild.transform.localPosition = new Vector2(itemChild.transform.position.x, itemChild.transform.position.y + 250);

【问题讨论】:

  • 您的代码根本没有设置父母的位置。您需要提供父母搬家的证据。

标签: unity3d position transform parent-child


【解决方案1】:

父母应该站着不动。你确定父母带着孩子搬家吗?我的意思是你只是在创建一个空的游戏对象,可以与我们分享 parent 和 child 的 x,y,z 吗?我尝试了您的代码并进行了一些更改,它对我有用。这是我的代码:https://imgur.com/khXrZ07 如果您只是创建一个空的游戏对象并在画布中单击它,因为您的父对象(在您的情况下为“go”)是空的,它只会专注于您的子对象,它可能会让您感到困惑.

【讨论】:

  • " 因为你的父母(在你的情况下是“go”)是空的,它只会关注你的子对象,它可能会让你感到困惑。你说的对!添加 SpriteRenderer 组件后,我可以看到 Parent 没有跟随子位置...谢谢!
  • 很高兴听到这个消息。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-04-13
  • 1970-01-01
  • 1970-01-01
  • 2021-10-17
  • 1970-01-01
  • 2012-12-17
  • 2011-04-30
相关资源
最近更新 更多