Parkour game obstacle coins generation

Two days ago, the teacher asked us to make a cool running game. The main content of this article uses the object array to generate obstacles and gold coins in a loop

First of all, you need to design the positions of obstacles and gold coins that need to be generated on the map and make them empty objects, and set unified labels for gold coins and obstacles;

In the code segment, first you need to declare 2 object arrays in the code segment (I wrote it relatively simply, if you need a rigorous layout, you can write an object array for each obstacle, and use empty objects with different labels to receive)

The object array of gold coins and obstacles and the prefabs of four obstacles are declared here.

Set the corresponding label in the resource manager class to facilitate later management 

Here is the prefab label of the empty object position and gold coin obstacle obtained to generate gold coins and obstacles 

Every empty object is traversed here, obstacles are randomly generated, and gold coins are all generated for empty objects

IEnumerator CloneCoin()
	{
		yield return new WaitForSeconds(1f);
		for(int i = 0; i < Coin.Length-1; i++)
		{ 
		    GameObject go=Instantiate(cion, Coin[i].transform.position, Quaternion.identity);
            go.transform.SetParent(Coin[i].transform);//把生成的物体变成生成位置物体的子物体	
        }
		for(int j= 0; j < Item.Length-1; j++)
		{
			int a=Random.Range(1,5);
			if (a == 1)
			{
				GameObject go= Instantiate(lz, Item[j].transform.position, Quaternion.identity);
				go.transform.SetParent(Item[j].transform);//把生成的物体变成生成位置物体的子物体				
            }
            if (a == 2)
            {
                GameObject go = Instantiate(dingz, Item[j].transform.position, Quaternion.identity);
                go.transform.SetParent(Item[j].transform);//把生成的物体变成生成位置物体的子物体              
            }
            if (a == 3)
            {
                GameObject go = Instantiate(car, Item[j].transform.position, Quaternion.identity);
                go.transform.SetParent(Item[j].transform);//把生成的物体变成生成位置物体的子物体              
            }
          
            if (a == 4)
            {
                GameObject go = Instantiate(zl, Item[j].transform.position, Quaternion.identity);
                go.transform.SetParent(Item[j].transform);//把生成的物体变成生成位置物体的子物体
            }
        }
	}

Here I give a random number to generate different numbers, and the corresponding empty objects generate different obstacles, and let the generated obstacles become the sub-objects of the empty object, and the empty object is set as the sub-object of the map, so that obstacles can be realized Objects move with the map.

I am not proficient in using object arrays, and welcome everyone to criticize and correct! !

Guess you like

Origin blog.csdn.net/Optimistic_lx/article/details/129379013