Collisions between the asteroids and the ship
The logic to control the collision of the ship with the asteroids is practically identical to the last section’s. We’re going to start by creating another script inside Collisions with the name Spaceship-Asteroids. Go into it, create an input connector, and call them both on and off. Go back to this level and connect them to the on and off connectors.
Since we’re going to need to access the asteroids, drag the asteroids collection (from the shared bar or from the variable from the Shots-Asteroids script) over the script we just created.
Go into Spaceship-Asteroids to create the logic. Since we want to process the collision in each cycle, drag out a KeepActive and connect it to on and off.
Since we’re looking for collisions between an object (the ship) and the objects of a collection (asteroids), we’re going to use a BoundingCollision of the object-objects type, just like we did in the case of the shots. Drag one out to the logic panel.
The object which we want to check the collisions with is the ship, so we need to drag the Spaceship object from the AssetManager panel to the object input parameter in BoundingCollision.
And the collection that contains the objects which we want to compare with is asteroids, which, since we’ve already shared it, is available in the shared variables bar. Drag it to the second parameter.
With this, we are now comparing in each game cycle if the ship collides with an asteroid. If a collision is detected, we’re going to change the ship’s animation, going from fly to explosion. However, unlike how we did it with the shots, on this occasion, we’re going to use the AnimationSelector box in the Sprite category in the Blackboxes tab. Drag it to the panel.
This box show the animations as activators when you drag a sprite to its input parameter. Drag the Spaceship variable to the input parameter.
You’ll see that the sprite animations now appear as activators by passing through the parameter, in this case fly and explosion.
Since we want an animation of the explosion of the ship to play when the ship hits an asteroid, connect the true trigger in BoundingCollision to the explosion activator in AnimatorSelector.
To finish the collision, we’re going to hide the ship when the explosion animation finishes, for which we’ll use the finished trigger. Drag the Spaceship variable to the panel and select the setVisibility action. Double-click on the visibility parameter and change its content property to false. Finally, connect the finished trigger in AnimatorSelector to the do activator.
Wow! We now have the collision between the ship and the asteroids. Check it out in the preview.08/03/2016 / No Comments
My first game
- My first game with WiMI5
- Signing up
- Before getting started
- Designing the game
- The background
- The ship
- The asteroids
- Collisions between shots and asteroids
- Collisions between the asteroids and the ship
- Collisions between the ship and the edge of the scene
- Game over
- And now what?