Day 5 of Getting Started with Unity: Player and Enemy Spawn, Base Explosion Animation

This article describes how the character is generated at the beginning of the game, as well as the triggering and setting of the base explosion animation

Today is the fifth day. Although only a little game details have been updated, it took a lot of work to debug

I don’t know why my browser can’t listen to the song, but I can on my phone. Chrome open without trace directly does not display, dementia.jpg

06/22 Update, Chrome can already listen to songs

## Explosion animation when the base is hit In yesterday's study, we only changed the flag of the base, but ~~Art is explosion~~, so I added the explosion effect today:
1
2
3
4
5
6
7
//Explode animation (the prefab needs to be loaded in the Project interface)
public GameObject explosionPrefab;
public void Die()
{
sr.sprite = BrokenSprite;
//Add the scene of generating explosion animation on the basis of yesterday:
Instantiate(explosionPrefab, transform.position, transform.rotation);
--- ## Player and enemy spawn In order to make the game look more immersive, the spawn effect is played before the player and enemy spawn, and the player/enemy will spawn after a second.

First create two enemy prefabs SmallEnemy and BigEnemy, copy the player’s script player to the new script enemy, update the prefabs in the script (such as transition patterns, bullets, special effects, etc.) .

Create a new script Born for the prefab Born_0 (don’t know why it had the suffix _0 at the time):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public class Born : MonoBehaviour
{
//Load the player's prefab on the project interface
public GameObject playerPrefab;
//Load the enemy's prefab (two) on the project interface
//Use a list to randomly generate one of two enemies
public GameObject[] enemyPrefabList;
//Determine whether Born created a player in the hierarchy interface
public bool createPlayer;

// Start is called before the first frame update
void Start()
{
//Delayed call (more immersive)
Invoke("BornTank", 1f);
//delayed destruction
Destroy(gameObject, 1f);
}

void Update()
{}

private void BornTank(){
//if creating a player
if (createPlayer){
//Generate the player's prefab (Quaternion.identity refers to Quaternion(0,0,0,0))
Instantiate(playerPrefab, transform.position, Quaternion.identity);}
else{
//int generates random numbers that only include the first number (0), not the second number (2): 0 or 1
int num = Random.Range(0,2);
Instantiate(enemyPrefabList[num], transform.position, Quaternion.identity);
}
}
}

The problem encountered today

With the deepening of learning, I will start to debug every day

For the 2019 version of Unity, when I copy and paste player in hierarchy to generate Enemy, I found that I cannot directly delete the shield prefab under player. If I forcibly delete the shield under Enemy (that is, copy the gift of player), the shield under Player in Project will also disappear, which means the player in hierarchy The shield is also gone.

After exploring to no avail, I had to start over by creating a new 2DObject and that solved the problem.