Generate Coins in ANY Shape like Jetpack Joyride – Unity Tutorial
Articles Blog

Generate Coins in ANY Shape like Jetpack Joyride – Unity Tutorial

December 14, 2019

Hello Fellow Game Developers
I hope you are doing good and in this tutorial we are going to learn how to generate coins in
different shapes just like in Jetpack Joyride. So let’s
jump right into it first of all I am going to create a new empty gameobject
on which we are going to attach our script and I’m going to call it coin generator
and I’m also going to create a new C# script to attach to our game
object and I’m going to call it Coin Generator also. Let’s get right into
coding part because that’s where all the magic happens. We don’t need a Update
function for this so I’m going to get rid of it first and I’m going to create
a new variable of type Texture2D which is going to store our Coin Map and we
are going to create a new helper class which is going to store our color and
prefab let’s call this class color to prefab and it doesn’t need to be derived
from MonoBehaviour so we are not going to mention that and inside it we will
have two variables first one is going to store the prefabs and the second one is
going to store the color and since we’re not deriving it from MonoBehavior so we
need to mark it Serializable in order to access it from the inspector
window so to do that I’m going to write system dot serializable and with that it
will show up in the inspector window now we can create an array where each
element in the array is a class that we just created and we are going to use it
to store different prefabs for different colors and once we have that in place we
are ready to calculate the location of pixels in our coin map for that we are
going to write a new function and I’m going to call it generate map and we are
going to identify the location of pixels by looping through the width and height
of the map and if the color of pixel matches our condition then we are going
to instantiate the corresponding coin at its position to loop through the image
we are going to create two FOR loops one will loop through the X coordinates and
the second one will loop through the Y coordinates and for each loop we are
going to call the function which is going to generate coins at the
given coordinates of X and Y and I’m going to call this function
generate Coins and it is going to take X and Y values from the loop and these
are the coordinates of the pixels now let us create this function and it will
take two arguments that is X and Y and both are of type INTEGER now this is a
function where we match the pixel color and instantiate the coins accordingly so
first of all we are going to get the color of the coordinate and for that we
have a function in unity it is called get pixel and we are going to store that
information in a variable of type of color so now all the color information
of the X & Y coordinates of `Coin Map` is stored in `Map Color` variable so we are
going to loop through each element of our ColortoPrefab array so for that
I’m going to use for each loop and if color of the current element of array
matches `Map Color` variable then instantiate prefab which we have in OBJ
variable at position of pixel coordinates which are X and Y so I am
going to create a new Vector to variable to store the value of X and Y and we are
going to use this in our instantiate statement and we don’t need any rotation
to it and I’m going to parent it to our new game object called `parent obj` and
come up here to declare a new variable by the name of `parent obj` so all the
coins are going to be child of parent obj so now when we come back to unity and
attach the script over game object we’re going to see couple of fields here the
first one is the `Coin Map` in which we are going to store our texture map for
the coins this one is the array of the helper class that we created so I’m
going to bump up the value to 1 so now each element has a prefab field and a
color field so what this means is that we are going to instantiate this prefab
wherever the pixel color is BLACK and the third field is going to take the
game object which will work as parent for all the coins now let’s jump in
Photoshop and create a map for our coins I’m going to create a new document of
size 48 by 8 pixels and select the pencil tool make sure the size is set to
1 and hardness is set to 0 and remember to create a new layer so that we don’t
mess up so let’s draw some pixels on this map and we’re going to use it in
Unity I’m going to turn off the background layer and save this file in
PNG format so now jumping back to unity we’ve got our coin Maps folder here and
I have some pre-made maps in it and also the one we just created but before we
move on it is mandatory to make some changes to over texture map so the first
one being changing the filter mode tool no point and in compression we need to
set it to none and the most important one is to make sure that the Read/Write
enabled is checked on, otherwise the `GetPixel` function will not work because it
requires the Read/Write enabled checked on. So I’m going to hit apply and
start filling the fields with the right information so in the coin map I am going
to drag and drop the Coin Map that we created in Photoshop and here I have a
couple of prefabs of coins of different colors so I’m going to place the golden
one in the prefab field of array and for the color I am going to select Black as
we have Black pixels on our map and make sure to bump up the Alpha to 255 and
also we need an empty ParentObj under which all the coins are going to be
instantiated so I am going to create an empty game object
call it a parentOBJ And I’m going to assign it to our third field so now when we
play button, this is something we will have and this is exactly what we are
trying to achieve and I’m going to select the ParentObj and drag it to the
prefab folder to convert it into a prefab so that we can use it later any
time. We can do this if we have multiple colors in our map so here I have a map
which has two colors in it red and black and I’m going to place it
in ‘Coin Map’ field and increase the size of array to 2, the first element is
going to stay the same and the second one I’m going to change the color of it
to RED and make sure the value of color matches exactly the value of color in
the map otherwise it won’t work and for the prefab I’m going to use a coin of
RED color so now when we hit play button *WOOOAAAHHH*
This looks pretty so here I have a
scene set already in which I have a game object which is going to randomly
instantiate different shape of coins and each shape is going to get destroyed
after 2 seconds so when I play it now this is how it looks like and I think
this is a really awesome technique and you can create any shape with any object
with it. It doesn’t have to be coins it can be diamonds, it can be trash cans, it
can be rocks or I don’t know like stars just anything you want
Thank You for Watching guys I really hope you enjoyed this video and you can
also connect with me on your favorite social platforms the links are in the
description below and you can also support me by Subscribing to the Channel m/
so I see you very soon with another video until then take care 😀

Only registered users can comment.

  1. Great video! The audio quality has improved a lot however it is really quiet. Even with sound on max it is hard for me to hear you voice clearly.

  2. Your video is really good!

    I've learned how to generate coins, so you did a great job.

    I suggest you add subtitles, no offence but sometimes I can't understand what you're saying (Probably because of the accent).

    You can just add them through youtube itself.
    Btw you should try doing some challenges

Leave a Reply

Your email address will not be published. Required fields are marked *