0

所以在这里我有一张图片可以更好地理解我的问题。所以目前我有一些白色块,在白色块中我有一些青色边框。所以我现在要解决的当前问题是。每当白色框延伸到青色边框框之外时。我想调整它们的大小,使它们位于青色边框内。比如从顶部开始的五分之一。

我怎么解决这个问题 ?

提前致谢。

在此处输入图像描述

编辑

void Update() {

    if( numOfGeneratedGuideline < numOfGuidelineToGenerate ) {
        Generate_Guideline_Positons(numOfGeneratedGuideline);
        Generate_Platform_Positions_And_Scale(numOfGeneratedGuideline);
        Generate_Platforms(numOfGeneratedGuideline);
        numOfGeneratedGuideline++;
    }

}

void Generate_Guideline_Positons(int i) {

    float tempGuidelineOffset = groundHeight + ( guidelineOffset * ( i + 1 ) );

    guidelinePosX[i] = worldWidth / 2;
    guidelinePosY[i] = tempGuidelineOffset;
    guidelinePosZ[i] = 0;

}

void Generate_Platform_Positions_And_Scale(int i) {

    randomGuidelineNumber = Random.Range(1, numOfGuidelineToGenerate + 1);

    float tempPlatformPosXMin = ( worldWidth - guidelineWidth ) / 2;
    Debug.Log(tempPlatformPosXMin);
    float tempPlatformPosXMax = worldWidth - tempPlatformPosXMin;
    Debug.Log(tempPlatformPosXMax);
    float tempPlatformPosY = groundHeight + ( guidelineOffset * ( i + 1 ) );

    platformPosX[i] = Random.Range(tempPlatformPosXMin, tempPlatformPosXMax);
    platformPosY[i] = tempPlatformPosY;
    platformPosZ[i] = 0;

    platformScaleX[i] = Random.Range(minPlatformScaleRange, maxPlatformScaleRange);
    platformScaleY[i] = 1;
    platformScaleZ[i] = 1;

    //22 29 36 43 50

}

void Generate_Platforms(int i) {

    GameObject newplatform = Instantiate(platformPrefab, new Vector3(platformPosX[i], platformPosY[i], platformPosZ[i]), Quaternion.identity) as GameObject;
    newplatform.transform.localScale = new Vector3(platformScaleX[i], platformScaleY[i], platformScaleZ[i]);

}
4

1 回答 1

0

我不熟悉 Unity 的细节,但无论如何,您都需要对矩形进行测试。

假设cyan是青色游戏gray对象的矩形,是灰色游戏对象的矩形。请注意:leftortop属性可能是xandy属性,而rightandbottom属性可能是x + widthand y + height

if (gray.left < cyan.left) //Out of bounds on the left
    gray.left = cyan.left;
if (gray.right > cyan.right) //Out of bounds on the right / past the right edge
    gray.right = cyan.right;
if (gray.top < cyan.top) //Out of bounds on the top
    gray.top = cyan.top;
if (gray.bottom > cyan.bottom) //Out of bounds on the bottom / gray stretches below cyan
    gray.bottom = cyan.bottom;

当然,这是假设一个正 x 和负 y 世界。例如,底部比顶部大,左侧比右侧小。

于 2014-11-16T07:50:27.593 回答