[Review] Unity Game Programming vol.1

## Foreword
The code in this post follows this great material for beginners of Unity in Japanese
http://www.shuwasystem.co.jp/products/7980html/5301.html

## Introduction
I have been wondering how the 3D engine works since I have watched the famous movie named [Avatar](http://www.shuwasystem.co.jp/products/7980html/5301.html).
According to my research, it seems that three reputed game engine dominate the market, which is [Unity](https://unity3d.com/jp/), [Unreal Engine](https://www.unrealengine.com/ja/what-is-unreal-engine-4), [cocos](http://cocos3d.org/).
I understand each has own strength and weakness, and actually it depends on the individual preference which to pick. In my case, I have chosen Unity.
So in this post I will focus on the C# game programming on Unity.

## Prerequisites
1. Please finish this official tutorial to get the intuition of functionality provided unity!
https://unity3d.com/jp/learn/tutorials
2. Background Knowledge on OOP(object-oriented programming)

* nice short material
https://qiita.com/ikemonn/items/1386c2218b0ec4d75b3c

## Agenda
1. Playing with the GameObjects
    1. Stage Setting
    2. Scripting ~Moving Back and Forth~
    3. Scripting ~Rotating~

## 1. Playing with the GameObjects
### 1. Stage Setting
Below, this is my current view.
<img width="1440" alt="Screen Shot 2018-05-20 at 14.50.56.png" src="https://qiita-image-store.s3.amazonaws.com/0/240801/ddfc9364-53e5-65eb-f973-d4d072f98a67.png">

So, first of please reset the camera configurations wherever it is located, make sure to set the back camera to its origin position.
And then, create the 3D object, Cube which is located at the origin except z-axis, you can set 3 to z-axis!. 
From "Hierarchy" you can create the material to efficiently play with the objects in this scene. Now, paint the box as red! Then you can get the view as me!
Secondly, let's set move our light to the place as below.
<img width="584" alt="Screen Shot 2018-05-20 at 14.56.20.png" src="https://qiita-image-store.s3.amazonaws.com/0/240801/d06cae61-4748-365d-ffb8-02f415e11010.png">

Phew!! Not so hard, isn't it.
if you get stack at somewhere, please refer to the official docs.

### 2. Scripting
Now, we have done to jump into the scripting to control the game object in our scene. Firstly, we are going to script the movement of the reddish cube moving back and forth.
So, create the C# script in your project and drag and drop to the cube!
Then, as you know, you can set the script to it.
Before heading for actual programming, let's review the existence of the gameobject and its relationship with others, like Audio or collisions.
![unity14_diagram.png](https://qiita-image-store.s3.amazonaws.com/0/240801/cdc6070b-d593-b241-da74-a280b77a7880.png)
Image Reference: https://www.raywenderlich.com/4595/how-to-make-a-2-5d-game-with-unity-tutorial-part-2

Simply saying, the game object is a class giving you an access to its plenty of information, for instance, location, scale and so on. To be honest, it even contains the methods to move or do some stuff, so you can consider Unity as a kind of framework providing the access to its tremendous class living on their platform and allowing the collaboration with other creators/designers to create new objects as well.
Okay, enough of talking, let's get our hands dirty!!
Actually once you see the code, I think you can easily get it.

```c#
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class myscript : MonoBehaviour {
  int counter = 0;
  float plus = 0.1f;

//at the begining
void Start() {
}

//looping while playing
void Update(){
   Vector3 p = new Vector3(0, 0, plus);
   transform.Translate(p);
   counter++;
   if(counter == 100) {
   counter = 0;
   //switching the direction
   plus *= -1;
   }
 }
}
```
![Untitled.gif](https://qiita-image-store.s3.amazonaws.com/0/240801/f9a1b82f-7c56-e63b-acbf-9c9c5dd20388.gif)

How did yours go?? Well? Nice!!!

### 3. Scripting ~Rotating~
Next, let's rotate it together!

```c#
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class myscript : MonoBehaviour {
	// Use this for initialization
    void Start () {
	}
	
	// Update is called once per frame
	void Update () {
        var v = new Vector3(0.5f, 0.5f, 0.5f);
        transform.Rotate(v);
    }
}
```
![Untitled.gif](https://qiita-image-store.s3.amazonaws.com/0/240801/e31fe8f5-da77-49f9-3753-7ab86fccceba.gif)

So, as you can see the two scripts above, transform.Rotate(v); or transform.Translate(v); giving us an access to its movement given the parameters of 3d coordinates using Vector3.
Or, we can even change the scale of the object.

```c#
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class myscript : MonoBehaviour {

    float dx = 0.01f;
    float dy = -0.01f;

    // Use this for initialization
    void Start () {
 }
  
   // Update is called once per frame
 void Update () {
        Vector3 s = transform.localScale;
        if(s.x > 3 || s.x < 0.1) {
            dx *= -1;
        }
        if(s.y > 3 || s.y < 0.1) {
            dy *= -1;
        }
        s.x += dx;
        s.y += dy;
        transform.localScale = s;
    }        
}
```
![Untitled.gif](https://qiita-image-store.s3.amazonaws.com/0/240801/44423f48-9b93-26b2-ed7b-589ad527b80a.gif)