Daily Progress: Collectible

Esteban Ibarra
6 min readJul 9, 2021

--

The next challenge was to create a collectible for the game. We were given a set of assets and I found one called SciFi_Container_01 which could fit the bill, Let’s drag it into the scene.

I want to animate this, so just to be safe, we’ll create an empty, zero it out, and call it Collectible_Holder and place the Scifi container into it and zero the containers position as well.

THIS IS SUPER IMPORTANT! ZERO EVERYTHING OUT! It doesn’t matter if everything disappears, we’re going to prefab the holder, delete it from the scene and then immediately put it back in the scene.

As this is going to be a collectible, it of course would need a rigidbody with gravity turned off and kinematic on for good measure as well as a box collider with trigger checked. Place the rigidbody on the holder, and the box collider on the sci-fi box. The rigidbody will handle all colliders in its children.

A static collectible just floating in mid air is a little boring. Let’s animate it a little so it’s really floating! Caveat: It’s best to create animations zeroed out to avoid any complications in the future. Since we zeroed out the sci-fi box and placed it into the Collectibles holder, it shouldn’t be an issue now.

select the sci-fi container and create a new animation and then save the animation in the Animations folder under a new Collectible folder.

Then in the timeline, select record, and in the first frame, move it a little just to set the keyframe. and in 15 seconds, move it up, 30 seconds, move it down, and 45 seconds move it back up again. Then copy (ctrl-c) the first keyframe into the 1 minute mark. (ctrl-v)

The final animation is a little too harsh, that’s ok. We can fix it using the curves menu. I had a minor senior moment and couldn’t remember what axis was up, and the green y up arrow is staring at me in the face! I finally selected the y axis and pressed f to focus on it and started adjusting. Adjust the curves to your liking, it’s all subjective but I switched the 45 second mark down so it made a nice reverse s curve and now the collectible is floating smoothly! It’s really cool how unity offers advanced animation tools in the editor. No doubt this will be useful for more advanced character animations in the near future!

Great! Now zero out the Collectible_Holder transform to zero again, it will disappear again, don’t worry. It’s still in the scene, just at origin where we need it to be. Hit apply in the prefabs overrides. You can delete the current holder, and drag a new one out into the scene. Now is a good time to hit Ctrl-S and save your work.

If you drag more holder prefabs out, you’ll see them all animate!

Let’s tag our Player as Player before we do anything else:

Ok, let’s create a script for the Collectible_Holder!

The collectible only needs one function in it, and that’s the OnTriggerEnter function:

We’ll need to access the player. The player isn’t a singleton this time around so we’ll do things the old way:

We haven’t written it yet, but we’ll call upon a function to increase the collectible count.

At this point, Unity will probably complain there’s no function and throw a fit. Just put your hand on Unitys shoulder, look Unity in the eye and say ‘Fuggetaboudit’. We’ll write that function now in the player script and calm Unity down:

It would also help to create the variable and initialize it.

Finally, back in the Collectibles script, We’ll need to make the collectible disappear when the player touches it so let’s just turn it off in case the game manager wants to turn them on again for a reset.

Ok! Let’s see if it works!

Hmm…it’s not collecting…let’s see where the collectibles are.

Ah ok, let’s just move them in front of the player.

Now let’s see if it works.

Yes! We have a working collectibles system! Just for kicks, let’s show the collectibles on screen: Create a UIManager script, a UIText, and drag the UIManager onto the Canvas.

Let’s make the UIManager a singleton:

Now make a holder for the Collectibles_Text

Drag the Collectibles_Text into the holder:

Just to be fancy, let’s get some copyright free fonts from google. I chose Zen-Dots.

after downloading them, I dragged them into a newly created fonts folder in my assets

Before changing fonts in the text, let’s deal with the canvas scaler first. change it to Scale with Screen and Match it to .5

Selecting Collectibles_Text, let’s move the textbox to the upper left and anchor it there as well. Change the font color to white, and increase the font size. then we can choose the font by either dragging the font into the slot or hitting the little target inside of the selector as shown in the example animation.

Awesome! We’re ready to script! We’ll first initialize the text to say Collectibles: 0. Then we’ll have the player update it by calling a function.

Then in our players UpdateCollectibles function, well access the UpdateCollectibles function and throw our _collectibles variable to it as a parameter. It’s a little work to set up the singleton, but it really pays off later!

Let’s see it in action!

Awwwww Yeahhhhhhhhh!

Tomorrows challenge is a moving platform! Easy Peasy! See you then!

--

--

Esteban Ibarra
Esteban Ibarra

Written by Esteban Ibarra

cartoonist, game artist, and wanabe gamedev.

No responses yet