How to create the game floor with a random Prefab

Asked

Viewed 745 times

3

To create the floor, I did it this way:

I created a gameobject and then assigned an Sprite render, box Colider, rigidbody 2d and a script.

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

I’ve also created 2 other objects, one to add a new Prefab (which is my floor) and one to destroy it when you step outside the camera boundary as the player advances.

In the script you assign to Prefab I check if there is a collision with one of the 2 objects above and 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 a type of floor, but I have other 3 types of floor/ Prefab with Polygon Collider in place of the box Collider (for having a different shape).

How do I make the new floor to be added a 4 prefabs Random that I have?

To better understand what I wrote above, see the image (any resemblance to the super mario is just copying): inserir a descrição da imagem aqui

1 answer

2


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

1 - Map Class

add two components, let’s use them to draw a random number and put together a list.

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

Within the class put at least one variable, in this case chaoTipos It’s a list that will contain all your floors. 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 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 taking X,Y from the new position, if you have used the position list, you can take it from it.

You can do it, it’s identical to your code, the difference is that I hang everything that’s created on 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 from his father.

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

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

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

Inside 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 https://youtu.be/B_nXuYgfLh8 - is a procedurally created map before the game starts but the principle is the same.

-- sorry if there is an error in the code, I did it right in the stackoverflow editor

Browser other questions tagged

You are not signed in. Login or sign up in order to post.