-
-
Notifications
You must be signed in to change notification settings - Fork 32
Home
- Why do my asteroids not pass through the boundaries?
- Why does nothing happen when a bullet hits an asteroid?
If your asteroids are unable to pass through the boundaries, then this most likely means the two are objects are colliding, but we actually want collision to be disabled between these objects. To do this, we set the game objects to be on different physics layers so we can turn off collision between the two layers in the physics settings.
The asteroids should have their layer set to "Asteroid" and the boundaries should be set to "Boundary". After doing this, we can go to Edit > Project Settings > Physics 2D
and uncheck collision between Asteroid/Boundary
in the collision matrix.
In the Asteroid.cs
script, the code is checking when the asteroid collides with a bullet by comparing the tag of the incoming object. If nothing is happening when a bullet collides with an asteroid, it might be that you have forgotten to set the tag of your bullet prefab to "Bullet", or there could simply be a mistake in the name or spelling.
It is also possible that you are comparing the name of the incoming game object rather than the tag, gameObject.name
vs gameObject.tag
. It's generally a better practice to use tags over names.
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Bullet"))
{
//...
}
}