【问题标题】:When creating game object position is wrong when set创建游戏对象位置时设置错误
【发布时间】:2017-06-04 04:43:03
【问题描述】:

这是我的代码

GameObject tocreate = new GameObject ("newsstory1");
    tocreate.AddComponent<SpriteRenderer> ();
    tocreate.GetComponent<SpriteRenderer> ().sprite = newsbuilten;

    tocreate.transform.parent = notifcationparents.transform;
    tocreate.transform.localScale = new Vector3 (6, 6, 0);
    tocreate.transform.position = new Vector3 (80, 18 ,5);

我的问题是当我运行这个游戏对象的位置错误;这是图片

【问题讨论】:

  • 位置可能是相对于transform.parent 的? notificationparents.transform 对象的绝对位置是多少?
  • 0,0,0是向量3的位置

标签: c# unity3d


【解决方案1】:

这是正常的。不要担心您在编辑器中看到的位置。发生这种情况是因为您正在设置父游戏对象。

假设父 Object(notifcationparents) 位于:

51030

然后创建一个名为 "newsstory1" 的新游戏对象并将其位置设置为:

80185

新的 GameObject 将移动到该位置,但您会看到:

758-25

作为它在编辑器中的位置,因为这就是 (80, 18, 5) -- (5, 10, 30)。您只是看到相对于其父 GameObject 的位置。如果没有父 GameObject,就不会发生这种情况。

【讨论】:

    【解决方案2】:

    不要使用transform.parent,而是使用transform.SetParent()。 另外,首先设置游戏对象的位置,然后使用 transform.SetParent(parent, true) 设置它的父对象。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-14
      • 1970-01-01
      • 2023-04-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多