From the course: Advanced Unity 2D: Platformer Player Movement

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Destroying the fireball

Destroying the fireball - Unity Tutorial

From the course: Advanced Unity 2D: Platformer Player Movement

Start my 1-month free trial

Destroying the fireball

- Now, let's look into how we can destroy the fireball when it's been on the screen for a little bit too long. In this case, we're gonna go back into our fireball script. One way for us to keep track of whether the fireball has been on the screen for too long is how many bounces it actually makes. Here, we'll create a public property in order to keep track of that. Let's do "public int" and call this "bounces", and we'll set it to three. Next, we'll get rid of the update method because we're not gonna be using it. And let's take advantage of the collider's callback by implementing OnCollisionEnter2D. This will pass in a Collision2D and we'll call that "target". Now, in order to track the number of bounces, we're gonna wanna see if the object we're colliding with is below the fireball. So, in this case, let's make a condition and ask for the target.gameObjects.transform.position.y, and see if it's less than the fireball's transform.position.y. Now whenever we detect this type of…

Contents