samedi 26 août 2017

unity2d: randomly change position of gameobject after it has been looped

So, I am making this little game, where the character constantly moves upwards with an autoscroll camera. The character jumps from platform to platform and as soon as a platform or a background tile is nolonger visible, i loop it back up. I assigned a range to my plattforms in which a randomizer choses a value from so that the player gets an individual set of plattforms every time he or she starts the game. the problem is the looping: since i do the randomizing in the start() functions, the random poision of the plattforms is only assigned once and then looped and looped again and again. so the game gets kinda boring after a few loops with is like after 20 seconds :D

Here is my code:

private float randomFloat = 0;
private int subOrAdd = 0; 

// Use this for initialization
void Start () {

    subOrAdd = Random.Range(1, 10);
    randomFloat = Random.Range(0f, 1.4f);

    // randomly add or subtract height of object 
    if (subOrAdd < 6)
    {
        this.transform.position = new Vector2(transform.position.x, transform.position.y - randomFloat);
    }
    else if (subOrAdd >= 6)
    {
        this.transform.position = new Vector2(transform.position.x, transform.position.y + randomFloat);
    }

}

Basically, I am having a hardcoded range and then randomly decide to either add or subtract the number that came out of the range. so how would i make it so, that the objects that get looped always ask for a new position? Because start is only called once as you know and even after looping, the position remains the same. I hope I made myself clear here :)

Any help would be awesome!




Aucun commentaire:

Enregistrer un commentaire