2014-11-16 89 views
0

所以在這裏我有一個圖像將提供更好的理解我的問題。 因此,目前我有一些白色塊,並在白色塊內,我有一些青色邊框。 所以我現在想解決的當前問題是。 每當白盒延伸到青色邊框外時。我想調整它們的大小,使它們位於青色邊框內。比如從頂端開始的五分之一。當它擴展了限制區域時調整gameObject的大小

我該如何解決這個問題?

在此先感謝。

enter image description here

編輯

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]); 

} 

回答

0

我不熟悉團結的細節,但在任何情況下,你需要做一個測試的矩形。

比方說,cyan是Cyan gameObject的Rectangle,gray是Grey gameObject的Rectangle。只需注意:lefttop屬性可能是xy屬性,而rightbottom屬性可能是x + widthy + 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世界。例如。底部是比頂部更大的數字,左側是比右側更小的數字。

+0

我認爲生病給你提供了我的代碼 – JekasG