mercredi 5 janvier 2022

Random Script repeatedly going down

I am busy building a platformer(2d). And I really want procedural generation. I think that I came up with a smart system for spawning the platforms randomly(up for debate). But my platforms keep on picking to randomly spawn down. No matter how many times I restart the game eg :enter image description here

here is my code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlatformSpawner : MonoBehaviour
{
public GameObject[] platform;
public Vector2 SpawnGap;
public float randomspawn;
public float VerticalRandomness;
public float queueTime = 1.5f;
private float time = 0;
public Vector2 startspawn;
public GameObject flag;

Vector2 nextspawn;
Vector2 nextspawncentre;
Vector3 prevspawn;
public int platamount;
int amountspawned;
bool alreadyspawnedflag;
// Start is called before the first frame update
void Start()
{

    platamount = Random.Range(5, 15); // random amount of platforms in level
    alreadyspawnedflag = false; 
}

// Update is called once per frame
void Update()
{

    

    if (time > queueTime) // just for quality of life(so that if I maybe have 10000 platforms I can delay the spawning)
    {
        if(platamount >= amountspawned) // random amount of platform
        {
            GameObject go = Instantiate( platform[ Random.Range (0, platform.Length) ] ); // instantiating random object from list, to make it even more random(objects assigned in inspector)
            Vector2 randomrange = new Vector2(Random.Range(1f, -1f) * randomspawn, Random.Range(2f, -0.25f/*perferes up, but stil not working*/) * VerticalRandomness);//calculates random position

            Debug.Log(randomrange);// it is indeed choosing random pos, even on y but still giong down


            nextspawncentre = new Vector2(prevspawn.x + SpawnGap.x + startspawn.x, prevspawn.y + SpawnGap.y + startspawn.y); // calculates next spawn without randomness
            nextspawn = new Vector2(nextspawncentre.x + randomrange.x, nextspawncentre.y + randomrange.y);// adds randomness


            go.transform.position = transform.position + new Vector3(nextspawn.x, nextspawn.y, 0);// places object at nextspawn variable

            time = 0;// resets timer

            
            prevspawn = go.transform.position;
            
            amountspawned += 1;
            randomrange = new Vector2(0, 0);// tried reseting the random, but to no success
        }
        else if(!alreadyspawnedflag)// almost same logic as before just for the flag to reload scene(handeled in another script)
        {
            GameObject spawnedflag = Instantiate(flag);
            spawnedflag.transform.position = new Vector3(nextspawncentre.x + 1, 15, 0f);
            alreadyspawnedflag = true;
        }
        
    }

    time += Time.deltaTime;
}

}

look at comments explained, everything there.

In advance thanks for the help.

ps: Platforms do not have rigidbodies, just sprite renderer and boxcollider2d.




Aucun commentaire:

Enregistrer un commentaire