Unity: Adding an asteroid with rotation

Objective: We need to add an asteroid to the game and add some rotation to it.

First, we need to add the asteroid to the scene by click and dragging the asteroid sprite into the hierarchy. Next we need to add some components to create the prefab so we’ll add the 2D rigidbody and the 2D circle collider in the inspector. When you add these components make sure to turn on the is trigger on the collider so that later we will be able to shot and collide with it as well as in the rigidbody set the gravity scale to 0 so it doesn’t have any gravity and fall differently. You will also want to edit the collider so it fits the shape of the asteroid better. Next we will need a script to be attached to the asteroid so in your scripts folder create a new script then and the script to the asteroid.

Now that the asteroid is set up you can go ahead and drag the asteroid to the prefabs folder in your project and if you made the prefab first make sure you remember to override it at the top of the inspector on the asteroid prefab. Next we will add the rotation to the asteroid so, go into your asteroid script and first create a float variable that handles the rotation speed make sure you add the serialize field function to it so you can edit it in unity if you need to make any changes.

Lastly, go into update and use the transform.Rotate function to access the rotation of the object then move the vector forward multiplied by your rotation speed variable and multiplied by time.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Jake Boere

Jake Boere

7 Followers

I am hard working Game development student learning as much about game development and unity as possible.