实例化对象没有所需的比例

时间:2016-12-14 21:05:18

标签: c# unity3d

我试图让两个街区垂直重生,中间有间隙。我已经这样做了,当我点击播放时,其中一个块的宽度太短。这导致两个间隙。我怎么能阻止这种情况发生。我只想要两个街区之间的一个空隙。我目前使用的代码:

public Transform block;
public Transform player;
private float objectSpawnedTo = 5.0f;
public static float distanceBetweenObjects = 9.5f;
private float nextCheck = 0.0f;
private ArrayList objects = new ArrayList();
void Start () {
    maintenance(0.0f);
}


void Update () {
    float playerX = player.position.y;
    if(playerX > nextCheck)
    {
        maintenance(playerX);
    }
}

private void maintenance(float playerX)
{
    nextCheck = playerX + 30;
    for (int i = objects.Count-1; i >= 0; i--) 
    {
        Transform blck = (Transform)objects[i];
        if(blck.position.y < (transform.position.y - 30))
        {
            Destroy(blck.gameObject);
            objects.RemoveAt(i);
        }
    }
    spawnObjects(5);
}

private void spawnObjects(int howMany)
{
    float spawnX = objectSpawnedTo;
    for(int i = 0; i<howMany; i++)
    {
        Vector3 pos = new Vector3(6.0f, spawnX, 0);
        float firstRandom = Random.Range(1,8.6f);
        Transform blck = (Transform)Instantiate(block, pos, Quaternion.identity);
        blck.localScale+=new Vector3(firstRandom*2,0,0);
        objects.Add(blck);
        pos = new Vector3(-6.0f, spawnX, 0);
        blck = (Transform)Instantiate(block, pos, Quaternion.identity);
        blck.localScale +=new Vector3((8.6f-firstRandom)*2,0,0);
        objects.Add(blck);
        spawnX = spawnX + distanceBetweenObjects;
    }
    objectSpawnedTo = spawnX;
}

我附上了一张图片来展示我想要实现的目标。红色轮廓是移动屏幕。我也希望能够使用触摸向左或向右移动块。我将如何做到这一点。

image

0 个答案:

没有答案