【问题标题】:Inconsistent floats with Unity与 Unity 不一致的浮动
【发布时间】:2015-02-26 08:31:43
【问题描述】:

我有几个预制件用于随机生成“房间”,但相同的代码会导致宽度不一致:

顶部(northWall)的宽度应该与底部(southWall)的宽度相同,但显然是 3 倍。

这是实例化其他“墙”预制件的“房间”预制件(此时它们基本上都只是四边形)。

public float length;
public float width;
public float wallDepth;

public Transform northWall;
public Transform southWall;

void Start () {
    length          = Random.Range (5.0f, 25.0f);
    width           = Random.Range (5.0f, 25.0f);
    wallDepth       = 2.0f;

    transform.localScale = new Vector3 (width, length, 0.0f);

    Instantiate (northWall, new Vector3(transform.localPosition.x, transform.localPosition.y + (transform.localScale.y / 2) + (wallDepth / 2), 10.0f), Quaternion.identity);
    northWall.transform.localScale = new Vector3 (width, wallDepth, 10.0f);

    Instantiate (southWall, new Vector3(transform.localPosition.x, transform.localPosition.y - (transform.localScale.y / 2) - (wallDepth / 2), 10.0f), Quaternion.identity);
    southWall.transform.localScale = new Vector3 (width, wallDepth, 10.0f);

}

我要疯了吗?是不是太晚了,我错过了什么?

谢谢。

【问题讨论】:

  • 你在inspector中设置了什么northwall和southwall?
  • 我刚刚为每个人拖了一个墙预制件。 Wall 是一个带有盒子碰撞器的四边形。
  • 那么inspector里面的规格是什么?
  • 它们没有规格,它们是默认四边形,其位置和比例设置为相同的随机浮点数(根据上面的代码)。但正如您所见,南墙是正确的,北墙不是。它们是完全相同的预制件。

标签: unity3d unity3d-2dtools


【解决方案1】:

我已经解决了这个问题。我认为这是因为我为四面墙中的每一面都使用了相同的预制件。我创建了一个函数,它返回预制件的克隆并将其重新分配给原始四边形。这样,比例和位置变换仅适用于四边形的复制版本:

GameObject buildWall (GameObject source, float width, float length, float x, float y, float z) {
    GameObject clone = Instantiate (source) as GameObject;

        clone.transform.localScale = new Vector3 (width, length, 0.0f);
        clone.transform.localPosition = new Vector3 (x, y, z);

    return clone;
}

实例化现在看起来像这样:

northWall = buildWall (northWall, width, wallDepth, transform.localPosition.x, transform.localPosition.y + (transform.localScale.y / 2) + (wallDepth / 2), 10.0f);

我不知道这是否是最有效(甚至是正确)的方法,但它现在似乎有效。

【讨论】:

    猜你喜欢
    • 2016-01-24
    • 1970-01-01
    • 2020-08-08
    • 1970-01-01
    • 1970-01-01
    • 2019-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多