Sunday, November 19, 2017

How to start Unity programming with C#

In this post, suppose we are using Windows OS (8.1).

1. Install Unity


Download Unity from here: https://store.unity.com/
Personal version is free, so choose the personal version. When you get the installer, start installing Unity.

2. Open Unity And Add A Cube


You will see the Unity's window:


Right click on the "Untiled*" pane. Select cube from the menu.

Cube is added in the scene.


Now click "Main Camera" from the left pane. Then click "Inspector" from the right side.

Change the position from the Inspector. I changed it to "x:0, y:1, z:-3". It will change the default position of the Main Camera.

Now we will check if the position was really changed. Click the play button from the upper menu.


You can see the Main Camera's position is right in front of the cube, which is same as the position that we wrote in the inspector.

Now exit from the play mode by pressing the play button.


3. Apply Gravity To The Object


Click the cube and click "add component".

Click "Physics".

Then click "Rigid body".

Rigid body component is added to the cube.

Change the position (to make the cube float in the air).
Position: x0, y3, z0.

Run the code by pressing the play button.

The cube will fall by the gravity (and fall forever because there is no ground).

Now add plane under the cube and set the position of the plane x:0, y:0, z:0.

Run the code by pressing the play button. The cube will fall on the plane now.

To apply gravity and make objects collide with each other:

Falling objects need the following:
    1.Collider
    2. Rigidbody
Ground that receives the falling objects needs the following:
    1. Collider that fits the form of the ground.

You can find the collider in the inspector of the cube and the plane. Box and Plane have the collider by default, so we didn't add collider manually this time.
"Box collider" is the collider



4. Add Prefab


Drag and drop the cube object to Assets. You get a prefab of cube on the Assets view.

Or you can create prefab from the Assets view too. Right click on the Assets and click "Create" > "Prefab".

We get a Prefab on the Assets view. Drag and drop the game object on the Prefab.

Anyway, after creating the prefab of the cube, create an empty game object.
Right click on the left pane > "Create Empty"

Then you get an empty game object.

 And click the empty game object and from its inspector, click "add component".

Scroll down the menu and click "New Script" and name it "CubeControl". Then click "Create and Add".

Maybe automatically MonoDevelop or VisualStudio or both of them will be opened. If not opened, just double-click the script from the Assets view:


As you see on the code, Start() function is used for initialization. Update() is called per frame.
using System.Collections; using System.Collections.Generic; using UnityEngine; public class CubeControl : MonoBehaviour { // Use this for initialization void Start () { } // Update is called once per frame void Update () { } }

Change the script like this:
using System.Collections; using System.Collections.Generic; using UnityEngine; public class CubeControl: MonoBehaviour { public GameObject prefab; // Use this for initialization void Start () { } // Update is called once per frame void Update () { if (Input.GetKey(KeyCode.Z)) { GameObject.Instantiate(prefab); } } }

Then see again the Unity view and click the empty game cube. From the inspector of the empty game object, you can see that a "prefab" item is created.


Drag and drop the prefab of the cube to the "prefab" item of the CubeControl of the empty game object.


You will see that the cube prefab is attached to the CubeControl as its prefab.


Now look at the script again... According to the script, as long as you press "Z", the prefab is instantiated.
using System.Collections; using System.Collections.Generic; using UnityEngine; public class CubeControl: MonoBehaviour { public GameObject prefab; // Use this for initialization void Start () { } // Update is called once per frame void Update () { if (Input.GetKey(KeyCode.Z)) { GameObject.Instantiate(prefab); } } }

Now check if it really works.

As long as you press "z" key on the keyboard... the cube prefab is instantiated..!


If you change "GetKey" to "GetKeyDown", the cube prefab is instantiated "every time" you press z on the keyboard.
using System.Collections; using System.Collections.Generic; using UnityEngine; public class CubeControl: MonoBehaviour { public GameObject prefab; // Use this for initialization void Start () { } // Update is called once per frame void Update () { if (Input.GetKeyDown(KeyCode.Z))
{ GameObject.Instantiate(prefab); } } }

5. Move the main camera


Now create a script for the main camera. Click on "Add Component" of the main camera and add "New Script". The name is "CameraControl".




Open the script and change the code like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraControl : MonoBehaviour {
// Use this for initialization
void Start () {
     
    }

// Update is called once per frame
void Update () {
        if (Input.GetKey(KeyCode.UpArrow))
        {
            this.transform.Translate(Vector3.forward * Time.deltaTime * 2);
        }
        if (Input.GetKey(KeyCode.RightArrow))
        {
this.transform.Rotate(Vector3.up, Time.deltaTime*50);
        }
        if (Input.GetKey(KeyCode.LeftArrow))
        {
this.transform.Rotate(Vector3.up, -Time.deltaTime*50);
        }
        if (Input.GetKey(KeyCode.DownArrow))
        {
            this.transform.Translate( -1 * Vector3.forward * Time.deltaTime * 2);
        }
    }
}

And run the project:

Your main camera moves (by pressing up arrow, down arrow, right arrow, left arrow)!