Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

[Review] Unity Game Programming vol.1

More than 1 year has passed since last update.

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.
According to my research, it seems that three reputed game engine dominate the market, which is Unity, Unreal Engine, cocos.
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)

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.
Screen Shot 2018-05-20 at 14.50.56.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.
Screen Shot 2018-05-20 at 14.56.20.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
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.

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

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

3. Scripting ~Rotating~

Next, let's rotate it together!

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

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.

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

Rowing0914
興味のある分野は:強化学習と最適化です
https://rowing0914.github.io/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away