Thursday 28 January 2016

Asteroids in Unity pt 3

In the last episode I finished the bullet physics so they stopped interacting with the ship and effected the movement, now I'll be introducing the asteroids themselves.

To begin I added the asteroid.png to the scene and gave it the usual components, (Rigidbody2d and a collider). I then created a new script called 'AsteroidController' which would dictate the velocity and range, I wanted the asteroids to move much slower then the ship so the player could live for longer, and the game would be fairer.


At this point the asteroids would spawn in the center of the screen and would move slowly to the right, and since I hadn't attached the 'MovementWrap' script, they just kept floating into an unseeable oblivion.

I had a spawn timer set in place, along with a decay amount which would shorten the time between each asteroid spawning by 5%.

To rectify the spawning issue I made the asteroids spawn outside the cameras view, this meant that when I attached the 'MovementWrap' script they would be teleported to the edge of the screen.


Although the scrip teleports the asteroids, it doesn't effect the velocity so they keep their speed when entering the screen. The issue I ran into now was the asteroids would still collide with one another, and some would spawn on the left of the screen moving upwards.

To fix the asteroids that would get stuck moving up/downwards on the sides of the screen, I added some lines to the script which would make the asteroids rotate around the center of the screen. I used the 'x' axis to achieve this.

This solved the issue of asteroids spawning on the edges of the screen, however the issue still occurred when asteroids collide.

I could have simply just made the asteroid a trigger, that would stop any collision between the asteroids while the ship would still react when hit. But I decided to go into the physics2d options and added a new class variant called 'asteroid'. It opened up a new box that I could un-check to stop all asteroids from reacting to one another.

No comments:

Post a Comment