Unity – Spaceship Collisions

It’ll be a short update this week folks as a good chunk of today’s session was dedicated to preparing for upcoming portfolio presentations and organising a time slot for delivery.

As of last week we had an asteroid that could move with an automated script and a ship which could move using input from the player. However nothing in the scene interacted with anything else, time for that to change!

We quickly focused on two things, collisions and triggers. Collisions I technically used in my Unity 3D game to set player boundaries but they had no other function. Triggers I wanted to get into to make a jump scare but ended up spending more time on modelling the assets.

Naturally we want our ship to take damage, maybe lose a life or outright player death on hitting an asteroid. It certainly isn’t going to flag us down and offer us tea and biscuits before flying on. The game requires a means to know if you’ve hit the asteroid and this is where the collider comes into play. Our aim is, collisions will cause outright player death and cause the game/current scenario to reset. Applying all of this to the ship should cause the ship to react to anything with a collider, making it easier to add obstacles later.

 void OnCollisionEnter2D (Collision2D collision) {
 SceneManager.LoadScene (SceneManager.GetActiveScene ().name);

To do this we need to add a new namespace to the script called UnityEngine.SceneManagement. This allows us to reference different scenes in the game or in simpler terms, if the player dies we can reference to reset the current scene, if the player reaches a goal we could also reference a new level the player moves to. Apparently this wasn’t required in an older version of Unity and therefore a little more straight forward, not sure why this was changed.

When working in 3D Unity had multiple types of colliders, box, mesh etc and 2D is no different. There are box, circle, edge and polygon colliders, almost spoilt for choice! While a circle collider was perfectly fine for the almost circular asteroid, it didn’t quite work for the ship, making a collision area much greater than the size of the ship. For this we used a polygon collider which creates a path of vertices around an object, these can be tweaked and altered to get a close fit.


Here is the snippet of code used to trigger a move to a new level. As long as the collider on an object is set to ‘Is Trigger’ the ship will move to a new level rather than being destroyed/reset.

 void OnTriggerEnter2D (Collider2D trigger) {
 SceneManager.LoadScene (nextLevel);



As you can see above we can set what scene we want to move to by typing its name (or dragging and dropping) into the ‘Next Level’ string we created.

To finish this off Ant added some terrain to collide with. I’m not sure at all where this came from (he probably created it), so instead I had a dig on kenney.nl to look for some terrain assets. Found a couple and threw one in to test, along with making a new scene with just the spaceship in it. The following two gifs will demonstrate what happens under collide and trigger events.

The ship triggers a new empty scene.
The ship interacts with the collider and restarts the level.

When we come back to this we’ll be introducing a scrolling camera. I’ve already downloaded both space shooter asset backs from kenney.nl in preparation for populating the scrolling level with more obstacles/challenges!



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s