Day 4 of Getting Started with Unity: The Response of a Bullet Touching an Object

This article tells about the different situations when bullets encounter players, bases, brick walls and iron walls.

~~I can’t keep up my spirits these days, but when I continued to learn Unity yesterday, I encountered a bottleneck. The bullet didn’t have the effect of touching the object, which made it worse. Fortunately, after checking for a long time today, I finally saw it, and I wrote it later, hoping to take this as a warning. ~~

Player death handling

In the tank battle, both the player and the enemy can fire bullets. In order to~~ the player can die~~ distinguish different bullets, we set a boolean value: public bool isPlayerBullet;//default false, and take the switch before In the statementframe add:

1
2
3
4
5
6
case "Tank":
if(!isPlayerBullet)//If it is not the player's own bullet
{
collision.SendMessage("Die");//Call the "die" method in the player script
}
break;

In the player (tank) script, the Die method is as follows (⚠️The method name must be the same or it cannot be called!):

1
2
3
4
5
6
7
8
9
10
11
12
13
//Explode the prefab prefab:
public GameObject explosionPrefab;
// Tank's death method
private void Die(){
//Detect whether the player is in a protected state, if so, return directly without performing the following death effects
if(isDefended == true){
return;
}
//generate explosion effect
Instantiate(explosionPrefab, transform.position, transform.rotation);
//die
Destroy(gameObject);
}

Base attacked

Our expectation is that when the base is attacked, the eagle flag will become a battle-scarred flag, and again, the call to the defeat method is made in the switch condition:

1
2
3
4
case "Heart":
collision.SendMessage("Die");
break;
}

And after creating a new Heart script, we load the prefab and set the prefab to be called after calling the Die method:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class Heart : MonoBehaviour
{
//2D rendering needs to take the sprite renderer component
private SpriteRenderer sr;
//Add the picture when the game is lost
public Sprite BrokenSprite;

// Start is called before the first frame update
void Start()
{
sr = GetComponent<SpriteRenderer>();
}

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

public void Die()
{
sr.sprite = BrokenSprite;
}
}

Brick and iron walls are attacked

When a brick wall is hit by a bullet, both the bullet and the brick wall should disappear; when an iron wall and an air wall are hit, only the bullet disappears:

1
2
3
4
5
6
7
8
case "Wall":
Destroy(collision.gameObject);//If the wall is touched, the game object (wall) will be destroyed
Destroy(gameObject);//Destroy itself
break;
case "Barrier":
//When hitting the wall and the air wall, only the bullet can be destroyed, and it has no effect on the obstacle
Destroy(gameObject);
break;

ridiculous problem

  • The first problem is that when I wrote OnTriggerEnter2D, the system did not allow me to write private. At first, I thought it was a version problem and didn’t care. Later, when I actually ran it, I found that this method was not called at all, and it was set to private void and an error was reported. After repeated comparisons, I found that I wrote this method in Update(), which is really speechless…
  • The second thing is that my bullet can still pass through the wall after writing the above code. After confirming that the code is correct, after a long, long investigation, I found out that it was because I installed a Box on the objects in the hierarchy Collider 2D, Rigidbody 2D, but not installed in prefab, nor update the picture of Sprite Renderer in the prefab script, even if I override many times, but these few places It hasn’t been updated, I don’t know if it’s the wrong way for me to open it…
  • The experience is that things updated in hierarchy in the future should remember to check in prefab to see if they are synchronized!