How to create the game floor with a random prefab

3

To create the floor, I did this:

I created a gameobject and then assigned a sprite render, box colider, rigidbody 2d, and a script.

I created a prefab and dragged the gameobject that I set up above the prefab and ready, so I created my floor and the prefab that will be added to the game as the player advances.

I also created 2 other objects, one to add a new prefab (which is my floor) and another to destroy it when I leave the camera border as the player advances.

In the script that assigns to the prefab I check if there is any collision with one of the 2 objects above and I remove or add a new prefab:

void OnTriggerEnter2D (Collider2D o)
    if (o.tag == "CreateGround"){
        Instantiate(Resources.Load("Prefabs/Ground"), new Vector3 (5.4f, -4.574f, 0), Quaternion.identity);
    else if (o.tag == "DestroyGround")
        Destroy(gameObject);
}

It works perfectly for one type of floor, however I have 3 other types of floor / prefab with polygon collider in place of box collider (for having a different shape).

How do I make the new floor to be added a random of the 4 prefabs I have?

To better understand what I wrote above, see the image (any resemblance to super mario is just copydence):

    
asked by anonymous 20.08.2015 / 03:42

1 answer

2

Can I make a suggestion? Create a class that will manage your 'Map' Otherwise jump to part two.

1 - Map Class

Add two components, let's use them to sort a random number and assemble a list.

using Random = UnityEngine.Random //Para sortear um número aleatório
using System.Colections.Generic //opcional para listas.

Within the class place at least one variable, in this case chaoTipos is a list that will contain all your floor types. You can also use a list for powerups, enemies and so on.

public GameObject[] chaoTipos; //se quiser uma aleatoriedade maior crie uma variavel para cada componente que forma o chão e você pode combinar eles ex:powerups, armadilhas, inimigos --- essa lista você pode arrastar direto no Inspector.

Then a variable to hang your floor, so everything will be organized in the hierarchy.

public Transform cenario; //vamos pendurar todos os pedacos aqui
public List <Vector3> posições = new List<Vector3> //lista das possíveis posições do cenário. Vai ser util se o mapa mudar a cada fase

Method

Each time you create a new component, I recommend you take the X, Y of the new position, if you have used the list of positions, you can pick it up.

You can do it, it's identical to your code, the difference is that I hang everything that is created in a parent component to have more control over it:

GameObject paraInstanciar = chaoTipos[Random.range(0,chaoTipos.Length] //pega um dos tipos de chão que você tem
GameObject instancia = Intantiate(paraInstanciar, new Vector3(X,Y,0f),      Quaternion.identity) as GameObject; //Instancia um deles
instancia.transform.setParent(cenario); //se seguir a parte um, para pendurar tudo relativo ao tabuleiro

And to remove it would just take him away from his father.

2 direct in the code (or, just updating what you sent in the question)

Before calling the method (as I explain in part 1)

public GameObject[] chaoTipos //pendure todos os tipos aqui

Within the trigger you use.

void OnTriggerEnter2D (Collider2D o)
if (o.tag == "CreateGround"){
    GameObject paraInstanciar = chaoTipos[Random.range(0,chaoTipos.Length)];
    Instantiate(paraInstanciar , new Vector3 (5.4f, -4.574f, 0), Quaternion.identity);
else if (o.tag == "DestroyGround")
    Destroy(gameObject);
}
  

Source link - is a map created procedurally before the game starts but the beginning is the same.

- sorry if there is any error in the code, I made it straight to the stackoverflow editor

    
20.08.2015 / 06:57