Unity: Spinning Power-up
Published: 2020-03-30 00:35:15

---
title: Unity: Spinning Power-up
published: true
description: 
tags: 
---
First, what is a spinning power-up? In many action games you may find small items boost your characters abilities, or give them new ones. It is often useful to make these items noticeable to the player, and make them stand out to the player doesn’t overlook them. You could use anything to represent these items, and you might have your own special models or icons, but here is a simple tutorial to make a spinning power-up from scratch in Unity. Your power-up can be any shape, but for this example we’re going to use a classic ‘capsule’ shape. So let’s start by creating a capsule object from ‘create’ menu in the ‘hierarchy’ panel.

By default your capsule will have 5 components, Transform, Mesh Filter, Mesh Renderer, Capsule Collider, and a Material. Let’s discuss each of these in turn. Transform is a combination of the objects location in 3d space, it’s rotation, and the scale of the object based on it’s original size. The Mesh Filter selects which mesh to use for rendering, and passes it to the Mesh Renderer. The Mesh Renderer renders the geometry provided by the Mesh Filter. The Capsule Collider handles collisions with the object. Normally this means that things will collide and not pass through, but if the option ‘Is Trigger’ is checked then it will have allow other object to pass through, but trigger a condition that may be referred to in scripts. The Material is the texture covering the object, by default a simple color. Now that we’ve cover what these things do, let’s see how we make our capsule a little more impressive.

Don’t worry about the Transform too much, you can place it where ever it needs to go, and scale it to match the scale of your environment. For this example, I’m going to scale my capsule to 0.15 in all directions. My Capsule starts standing straight up, but that’s boring. We want the spin to be noticeable, so we’ll set it to a slight angle to the spin will be more dynamic, so we’ll set the Rotation Z to 45. But before we make it spin, let’s make sure it looks nice.

####Making it Pretty

Next we’re going to make the capsule a different color than the standard grey, something that pops a little more and draws the players eye. Unfortunately it’s not quite as simple as changing a color picker. Almost, but not quite. We’re going switch to the Project tab for a moment, and here we’re going to use the ‘create’ menu to make a new ‘Material’. Name it whatever you want, let’s call it ‘power_up_color’. This is going to be the material that covers the surface of the capsule. Select your new material, and in the inspector panel you’ll see a bunch of options. Look at the top, under ‘Main Maps’ is Albedo, which has a color picker next to it. We’ll set it to a nice bright blue for this example. You could adjust the ‘Metallic’ and ‘Smoothness’ options if you want it to be a little more shiny. Now that we’ve made a nice material, let’s go back to our capsule. In the Mash Renderer is the field ‘Element 0’, which will be set to ‘Default-Material’. Click the small circle to the right of this to get a selection window where you can choose your material, and we’ll select the material we just created. Now our capsule is blue! Also, now that we’re using a custom material, you can edit it directly by selecting the material component.

Okay, our capsule is blue, but we can make it pop even more. In fact, we can actually make it glow! Let’s start by making a Light component using the ‘Add Component’ button at the bottom of the Inspector window. You can find it under ‘Rendering->Light’ or by searching for Light. Set the range to 0.4, and the color to something very similar to whatever color you’ve made your capsule. Also check ‘Draw Halo’, so that the light will show up without being on a surface. Now the capsule is colorful and glowing. Now let’s make it spin.

####Making it Spin

So how do we spin it? We’re going to use a simple script, so we’ll start by making a new script. In the Project window, use the ‘create’ menu and make a new Script, we’re going to call it PowerUpSpin. Select the script and int the Inspector click ‘open’ to open it in your default script editor. For this behavior we’re only going to need the `Update` function, All we need to do is tell it to update its Transform Rotation, and since we’re putting this in Update it will occur every frame:

```C#
void Update() {
	transform.Rotate(new Vector3(15, 30, 45) * Time.deltaTime);
}
```         

Great, we have a script, so let’s attach it to our capsule. With the capsule selected, click ‘Add Component’ and under ‘Scripts’ you should be able to find your custom script. And there you have it, Your glowing capsule will now rotate in place, and shine like a beacon to alert players to its presence.