【问题标题】:Using boxcollider2d.bounds.size.x gives me the wrong size使用 boxcollider2d.bounds.size.x 给了我错误的大小
【发布时间】:2020-10-21 22:20:31
【问题描述】:

我正在尝试使用 boxcollider2d.bounds.size.x 在通过使用生成平台进行实例化时将 2 个平台“捕捉”在一起

spawnPosX = lastSpawnedPlatform.transform.position.x + UnityEngine.Random.Range(0.5f, 2) + width;

width = boxcollider2d.bounds.size.x

但尺寸太小而且平台重叠。如果有人有解决方案,我会很高兴听到,如果是这样,请@我

我尝试将宽度声明移动到没有成功

完整代码:https://pastebin.com/7Rn1dEBh

【问题讨论】:

    标签: c# visual-studio unity3d game-development


    【解决方案1】:
        GameObject boundsSizer = Instantiate(choosenPlatform, new Vector2(99, 99), transform.rotation);
        choosenPlatformWidth = boundsSizer.GetComponent<BoxCollider2D>().bounds.extents.x;
        DestroyImmediate(boundsSizer);
        //jävla skit lösning här men men
        float spawnPosX;
        if (canDoubleJump == true)
        {
            spawnPosX = lastSpawnedPlatform.transform.position.x + /*UnityEngine.Random.Range(0.5f, 2) +*/  lastSpawnedPlatformWidth + choosenPlatformWidth;
        }
        else
        {
            spawnPosX = lastSpawnedPlatform.transform.position.x + /*UnityEngine.Random.Range(0.5f, 1.25f) + */lastSpawnedPlatformWidth + choosenPlatformWidth;
        }
        return spawnPosX;
    

    通过在两个对象上获取 bounds.extent 来解决问题,但为了得到这个,我必须实例化对象以使用 bounds.extent 然后使用 destroy imidiate 非常糟糕的解决方案,但它有效

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-11-10
      • 2017-10-11
      • 1970-01-01
      • 2010-10-29
      • 1970-01-01
      • 2016-06-25
      • 1970-01-01
      相关资源
      最近更新 更多