Monthly Archives

April 2017

Understanding “Vector” in Unity

By | Unity3D | No Comments

Definition:  In unity, Vector is the data type where you can do all types of vector calculation (Dot product, Cross product, Normalize, Projection, Magnitude) and other unity given function such as “Distance” & “MoveTowards” etc. As the “Distance” and “MoveTowards” are highly used in our “Game Development” process. We will try to describe as much as possible down below.

 

  • Distance: It calculate the distance between 2 vector point. We usually use it for detection or creating a sensor like a game object will react when the user comes to a certain point.

Parameter:         Vector2.Distance(Vector2.position, Vector2.position)

Vector3.Distance(Vector3.position, Vector3.position).

 

Vector (1)

Here, the “Mine” will be explode when the distance between the “Player” & “Mine” will be less than or equal to “3.14”. As the “Player” is at “5.14” distance, so the “Mine” is not exploding. We can see the following script for more detail.


  • MoveTowards: It basically moves the current position of the game object to the targeted position based on the velocity that has been assigned in the “MoveTowards” function parameter.

Parameter:

Vector2.MoveTowards(Vector2.position (Current), Vector2.position (Target), Velocity).

Vector3.MoveTowards(Vector3.position (Current), Vector3.position (Target), Velocity).

               

Problem: Let us consider that we have a homing missile that chases the player. If the player is within its range, It will start chasing the player. Let’s see the following script reference.

 

 


# The reference of the documents are taken from the “Unity – Documentation”.

Muhammad Tashfiqul Bari

Mentor at Coursera

Understanding “Sprite Renderer” Component

By | Unity3D | No Comments

Definition: “Sprite Renderer” component is responsible for rendering Image/Sprites in 2D/3D game mode.

Sprite Renderer (0)

Let us consider you want to add a simple background to you level. All you have to do is to select a “Game Object” -> Go to “Inspector View” -> Click on the “Add Component” -> “Sprite Renderer”, and select the Image/Sprite from the “Sprite” section to displayed in the game level. You can also create a “Game Object” by right clicking in the “Hierarchy” -> “2D Object” -> Sprite.


Components: Here, we are going to learn about the sub-components of the “Sprite Renderer”.

  1. Sprite: Here, you will be able to select the “Image/Sprite” that you want to be displayed in your game level.

Sprite Renderer (1)

 

Problem: Let us consider you want to change the Image from one image to another when you pressed the “S” key.


  1. Color: By using the color, you will be able to cast a “Gradient/Shade” on your selected “Image/Sprite”. By default the color is set to white (r: g: b = 255 : 255 : 255). Let us consider the following difference between 2 sprites.

Sprite Renderer (2)

Problem: Let us consider we want to change the opacity to its half when the user pressed the “O” key from keyboard. Note that


  1. Flip: By using this sub-component, you will able to flip the image along with the X/Y-Axis. By default, let us consider your image is facing to the right side. When you flip it along the X-axis, the image will now face to left. Let’s see the following image for better understanding.

Sprite Renderer (3)

Problem: Let us consider we want to flip “Sparty” (Character) to left face when we click on the “Left Arrow Key”. As by default Sparty is facing to the right.

 


  1. Materiel: Here you can also use different custom materiel for giving different gradient on the following image. We will talk more about in “Understanding “Materiel”” section.
  2. Sorting Layer: it defines the order that which layer would be render over another layer. Let us consider that the “Sparty” waking alongside of a tree. Is the tree will be behind him when he pass? Or the tree will hide “Sparty” when he goes over?
  • Let’s select the “Sparty” from the “Hierarchy”.
  • We can see that the “Sorting Layer” is set to default. (Cause it’s the default sorting layer)
  • Let’s click on the dropdown section and click on the “Add Sorting Layer”.

Sprite Renderer (4)

  • As we can see that our “Inspector View” has changed, and showing the “Sorting Layer”. We can also see that “Unity” has already created the “Default” (Unchangeable) sorting layer which we saw in the “Sprite Renderer” of “Sparty” game object.

Sprite Renderer (5)

  • Let’s create a “Sorting Layer”, naming “Player” layer.

Sprite Renderer (6)

  • Now set the “Player” layer to “Sparty”.

Sprite Renderer (7)

  • Now as we can see that the “Sparty” is now rendering over the “Tree”. Because the “Sparty” is set to the “Player” layer, where as the “Tree” has set to “Default” layer.

Sprite Renderer (8)

  • You should remember that the “Layer” which is the last “Layer” of the list has the highest priority for rendering to the top of the other layer.

  1. Order in Layer: Let us consider there are 2 different images in the same “Default” layer, where both of the images are in the same position. Now the question is, which image would be at the top to render? This is where the “Order in Layer” comes in. The “Sprite” that has the highest number in the “Order in Layer” would be render on the top. Rest of the object will be rendered as following.

Sprite Renderer (9)


# The reference of the documents are taken from the “Unity – Documentation”.

Muhammad Tashfiqul Bari

Mentor at Coursera

 

Understanding “.unityPackage”

By | Unity3D | No Comments

Definition: Unity package is nothing but “.zip” format for the unity where you can directly “zip” and “unzip” your following game asset from the editor. Let us consider the fact that your friend sends you willing to send you a 100 sprites of enemy along with the script. He will just create those asset to “.unityPackage” and send that file to you which will be extract to your “Asset” folder which is displayed as a “Project” view in the “Unity Editor”.

We will show you how you can create and extract a “.unityPackage” in “Unity Editor”.

 

Export Package:

  1. Let us consider that we want to create our “Art” folder from “Project” view to a “.unityPackage”. I will show you the both way on how you can export a “.unityPackage”.

unityPackage (1)


  1. You can either “Select” the folder that you want to export, and “Right Click” on it. Where you can find an option call “Export Package..”.

unityPackage (2)


  1. Here, you can see that all the items are the “Art” folder has been selected to be exported in the “.unityPackage”. you can deselect any item if you wish not to be include in the “.unityPackage”. Now let’s create the “Export” button to export our art asset.

unityPackage (3)


  1. It will ask you to set a destination where it will export the “.unityPackage”. give your package a name and click on the “Save” button.

unityPackage (4)


  1. As you can see, that my “ArtAsset.unityPackage” is now ready to port on any unity game.

unityPackage (5)


Import Package:

 

  1. Let us consider that we want import the “ArtAsset.unityPackage” to our new project in unity. To do that, just create a new project in unity. ( I am skiping this section as I do believe you guys can create a project in unity).
  2. Now go to the “Assets” -> “Import Package” -> “Custom Package..”.

unityPackage (6)


  1. Now select your “ArtAsset.unityPackage” and click “Open” button.

unityPackage (7)


  1. Now it will show you the list of all the content of the following package, where you can select/deselect any items you wish to add, and what are the items that are new in your project (the reason why it’s showing “NEW” it’s because the following item is not in the “Asset” folder, if it doesn’t show “NEW” means, the item that you want to add is already in the “Asset” folder. By importing again, it will just override the file). Now, let’s click on the “Import” button to import all the asset to our “Project View” in “Unity Editor”.

unityPackage (8)

 

  1. It will take few seconds (Based on the asset size) to load all the item of the “.unityPackage”. Here you can see in our “Project View” that all the asset has been loaded successfully.

unityPackage (9)


# The reference of the documents are taken from the “Unity – Documentation”.

Muhammad Tashfiqul Bari

Mentor at Coursera

Understanding “Tag” in “Unity”

By | Unity3D | No Comments

Definition: Tag is basically grouping the different types of game object by assigning them with a name.

For example, let us consider that there are 2 types of enemies such as “square” and “circle”. When the player collided with any of those enemy, the player will die. So far that I have taught you guys, you might be thinking to type your codes like this,

void OnCollisionEnter(Collision other) {

if(other.gameObject.name == “square” || other.gameObject.name == “circle”){

Destroy(gameObject);

}

}

But what if, there are 20 types of enemy in your game, so are you going to type their every single name to your code? And what if there are 20 “circle” enemy in the game, where their naming comes to “circle (1), circle (2) …..” by copying in unity. So are you going to type all of their name through your code? Definitely not, this is where the tagging comes. We are going to create an “Enemy” tag from the “Tag” window in “Unity” and set that “Enemy” tag to our different type of “Enemy” game object. Let’s see how we can create and use tags on unity on the following steps.

 

  1. To create those tag, lets select a “Game Object”.

Tag (1)

  1. Here, as we selected “Enemy (6)” from the “Hierarchy”. As now we can see that the “Tag” is now set with “Untagged” means, that there is no tag for the following “Game Object”. Let’s click on the drop down menu of the “Tag”.

Tag (2)

Now “Click” on the “Add Tag…” section to creating a “tag” for the following “Game Object”.

  1. Now you can see that the following “Inspector View” has changed to “Tags & Layer” section.

Tag (3)

As we don’t have any “Tags” right now, so the following list is showing “List is Empty”. Let’s click on the small “+” (Add) sign icon to create our first tag.

 

  1. Let’s set our new tag name as “Enemy”, and click on the “Save” button.

Tag (4)

  1. Now we can see that the “Enemy” tag is now available on the “Tags” list.

Tag (5)

  1. Again, let’s select the “Enemy (6) from the “Hierarchy” and click on the dropdown menu of “Tags” to select the “Enemy” tag for our enemies.

Tag (6)

  1. After selecting the “Enemy” tag, it is now been displayed on the “Tag” section of the “Inspector View”.

Tag (7)

  1. As now we have set the enemies with the “Enemy” tag, we can write our code in something like this,

void OnCollisionEnter(Collision other) {  if(other.gameObject.tag == “Enemy”){         Destroy(gameObject);    }}

Where this script will now destroy the player (assuming this script is attached to “Player”) as it touches with any object containing “Enemy” tag.

 


# The reference of the documents are taken from the “Unity – Documentation”.

Muhammad Tashfiqul Bari

Mentor at Coursera