Tuesday, November 29, 2016

UNITY3D V.4.3.4 Moon Machine Part 3


UNITY3D V.4.3.4 MAIN CAMERA

STEPS

FINDING MAIN CAMERA

1) Click the item Main Camera in the Hierarchy Panel.
2) The Camera Icon and its Preview Window is shown on the Main Panel.
3) Position the camera at the back of the MoonMachine.
Use Move and Rotate tool to put the camera icon at the right place.
You will get a Camera Preview as below.
4) To attach the Main Camera to the Moon Machine (so that it follows wherever MoonMachine goes), drag the item into the MoonMachine group.
As a result, when you select all items in the MoonMachine and move them back and forth, the Camera Preview should reflect the movement.

REFERENCES:



UNITY3D V.4.3.4 Moon Machine Part 2


UNITY3D V.4.3.4 CREATE LIGHTS

STEPS

CREATE SPOTLIGHT

1) Go to menu
2) Using Move and Rotate tool, place the spotlight in the front of the MoonMachine

CREATE DIRECTIONAL LIGHT

3) Go to menu
4) Using Move and Rotate tool, put directional light on top of the MoonMachine.

CREATE POINT LIGHT

5) Go to menu
6) Using Move and Rotate tool, put point light at the bottom of the MoonMachine.

SET THE COLOUR OF THE LIGHTS

7) The light colour property is set in the Inspector Panel.
7.1) Set…
Directional Light: Light Blue
Spot Light: Yellow
Point Light: Orange
7.2) Check that the light button is turned on.
ATTACH LIGHTS TO OBJECT
8) To attach the spotlight and pointlight to the MoonMachine, select the items and drag them to the MoonMachine group in the Hierarchy Panel.

REFERENCES:

UNITY3D V.4.3.4 Moon Machine Part 1


UNITY3D V.4.3.4 CREATE 3D OBJECTS

STEPS

CREATE CUBES ENVIRONMENT

1) Start Unity3D v 4.3.4
1.1) Left-click – Move the scene
1.2) Right-click – Pan the scene
1.3) Scroll Up/Down – Zoom In/Out
2) Create Plane Object.
Set Position: X=0, Y=0, Z=0.
Set Scale: X=50, Y=50, Z=50.
3) Create Cube Object.
Set…
Position: X=-4, Y=0.4, Z=23
Rotation: X=14, Y=126, Z=51
Scale: X=19, Y=19, Z=19
4) Set the view.
Right-click the label below the view arrows.
Choose Right.
Choose Perspective.
Use Mouse:Left+Click and Mouse:Right+Click to get the view below.
5) Select Cube with Global Coordinate View.
5.1) Click Move button.
5.2) Click Cube.
5.3) Click Global Coordinate mode.
6) Create second Cube using duplicate technique.
6.1) (While Cube is selected) Press CTRL+D.
7) Move the second Cube to a new location.
7.1) Left-Click the Blue Arrow of the second Cube.
7.2) While holding Left-Click, move it to the left.
The cube duplicate is now placed at new location.
Set…
Position: X=80, Y=0.4, Z=-130
Rotation: X=14, Y=126, Z=-51
Scale: X=103, Y=103, Z=103
8) Create third cube (duplicate).
8.1) (While Cube is selected) Press CTRL+D.
9) Move third cube away.
Set…
Position: X=-109, Y=0.4, Z=-130
Rotation: X=14, Y=126, Z=-51
Scale: X=66, Y=66, Z=66
9) Create fourth cube.
9.1) (While Cube is selected) Press CTRL+D.
9.2) Set…
Position: X=-109, Y=0.4, Z=105
Rotation: X=14, Y=167, Z=-51
Scale: X=66, Y=66, Z=66
10) Create fifth cube.
10.1) (While Cube is selected) Press CTRL+D.
10.2) Set…
Position: X=98, Y=0.4, Z=68
Rotation: X=14, Y=167, Z=-51
Scale: X=53, Y=53, Z=53

CREATE CAR

11) Create Base Cube.
11.1) Go to menu GameObject/Create Other/Cube.
11.2) Resize the Base Cube.
Double-click the Cube item (to zoom to the object).
Click the Resize button.
Notice that the arrows are having different end point symbols.
This means that you can click the end point to resize the shape.
Final values should be…
Position: X=0, Y=0, Z=0
Rotation: X=0, Y=0, Z=0
Scale: X=3.1, Y=1, Z=1.7
12) Add Cylinder.
12.1) Go to menu.
Outcome:
12.2) Set values…
Position: X=0, Y=0, Z=0.9
Rotation: X=-90, Y=0, Z=0
Scale: X=1.6, Y=0.1, Z=1.6
13) Create second cylinder.
13.1) (While selecting the first cylinder) press CTRL+D.
13.2) Move to the other corner of the base cube.
13.3) Set values…
Position: X=1.4, Y=0, Z=0.9
Rotation: X=-90, Y=0, Z=0
Scale: X=1.6, Y=0.1, Z=1.6
14) Create the third and fourth cylinder (by duplicating first and second cylinder).
14.1) While selecting both first and second cylinder, press CTRL+D.
14.2) Move to the other sides of the base cube.
Adjust the position of the cylinder so that their centers are aligned to the base cube.

CREATE A GROUP FOR THE CAR

15) Create a new empty object.
15.1) Go to menu
15.2) Select (using Ctrl+Click) and drag the items into the GameObject item.
Outcome…
15.3) Rename the GameObject as “MoonMachine”.
Outcome…
Adjust the position of the MoonMachine so that it is located on the plane.
REFERENCES:

Unity3D Flip Card Game Part 4




Objective: To display white cards for the matching robots

File: GameScript.js


var cols: int = 4; // the number of columns in the card grid
var rows: int = 4; // the number of rows in the card grid
var totalCards: int = cols * rows; // I think the answer is 16, but I was never good with numbers.
var matchesNeededToWin: int = totalCards * 0.5; // If there are 16 cards, the player needs to find 8 matches to clear the board
var matchesMade: int = 0; // At the outset, the player has not made any matches
var cardW: int = 100; // Each card's width and height is 100 pixels
var cardH: int = 100;
var aCards: Array; // We'll store all the cards we create in this Array
var aGrid: Array; // This array will keep track of the shuffled, dealt cards
var aCardsFlipped: ArrayList; // This array will store the two cards that the player flips over
var playerCanClick: boolean; // We'll use this flag to prevent the player from clicking buttons when we don't want him to
var playerHasWon: boolean = false; // Store whether or not the player has won. This should probably start out false :)
function Start() {
    playerCanClick = true; // We should let the player play, don't you think?
    // Initialize the arrays as empty lists:
    aCards = new Array();
    aGrid = new Array();
    aCardsFlipped = new ArrayList();
    BuildDeck();
    for (i = 0; i < rows; i++) {
        aGrid[i] = new Array(); // Create a new, empty array at index i
        for (j = 0; j < cols; j++) {
            var someNum: int = Random.Range(0, aCards.length);
            aGrid[i][j] = aCards[someNum];
            aCards.RemoveAt(someNum);
        }
    }
}

function OnGUI() {
    GUILayout.BeginArea(Rect(0, 0, Screen.width, Screen.height));
    BuildGrid();
    GUILayout.EndArea();
    print("building grid");
}

function BuildGrid() {
    GUILayout.BeginVertical();
    GUILayout.FlexibleSpace();
    for (i = 0; i < rows; i++) {
        GUILayout.BeginHorizontal();
        GUILayout.FlexibleSpace();
        for (j = 0; j < cols; j++) {
            var card: Object = aGrid[i][j];
            var img: String;
            if (card.isMatched) {
                img = "blank";
            } else {
                if (card.isFaceUp) {
                    img = card.img;
                } else {
                    img = "wrench";
                }
            }
            GUI.enabled = !card.isMatched;
            if (GUILayout.Button(Resources.Load(img),
                    GUILayout.Width(cardW))) {
                if (playerCanClick) {
                    FlipCardFaceUp(card);
                }
                Debug.Log(card.img);
            }
            GUI.enabled = true;
        }
        GUILayout.FlexibleSpace();
        GUILayout.EndHorizontal();
    }
    GUILayout.FlexibleSpace();
    GUILayout.EndVertical();
}

class Card extends System.Object {
    var isFaceUp: boolean = false;
    var isMatched: boolean = false;
    var img: String;
    var id: int;

    function Card(img: String, id: int) {
        this.img = img;
        this.id = id;
    }
}

function BuildDeck() {
    var totalRobots: int = 4; // we've got four robots to work with
    var card: Object; // this stores a reference to a card
    var id: int = 0;
    for (i = 0; i < totalRobots; i++) {
        var aRobotParts: Array = ["Head", "Arm", "Leg"];
        for (j = 0; j < 2; j++) {
            var someNum: int = Random.Range(0, aRobotParts.length);
            var theMissingPart: String = aRobotParts[someNum];
            aRobotParts.RemoveAt(someNum);
            card = new Card("robot" + (i + 1) + "Missing" + theMissingPart, id);
            aCards.Add(card);
            card = new Card("robot" + (i + 1) + theMissingPart, id);
            aCards.Add(card);
            id++;
        }
    }
}

function FlipCardFaceUp(card: Card) {
    card.isFaceUp = true;
    if (aCardsFlipped.IndexOf(card) < 0) {
        aCardsFlipped.Add(card);
        if (aCardsFlipped.Count == 2) {
            playerCanClick = false;
            yield WaitForSeconds(1);
            if (aCardsFlipped[0].id == aCardsFlipped[1].id) {
                // Match!
                aCardsFlipped[0].isMatched = true;
                aCardsFlipped[1].isMatched = true;
            } else {
                aCardsFlipped[0].isFaceUp = false;
                aCardsFlipped[1].isFaceUp = false;
            }
            //aCardsFlipped[0].isFaceUp = false;
            //aCardsFlipped[1].isFaceUp = false;
            aCardsFlipped = new ArrayList();
            playerCanClick = true;
        }
    }
}

Unity3D Flip Card Game Part 3



Objective: To allow player to flip two cards

File: GameScript.js

var cols: int = 4; // the number of columns in the card grid
var rows: int = 4; // the number of rows in the card grid
var totalCards: int = cols * rows; // I think the answer is 16, but I was never good with numbers.
var matchesNeededToWin: int = totalCards * 0.5; // If there are 16 cards, the player needs to find 8 matches to clear the board
var matchesMade: int = 0; // At the outset, the player has not made any matches
var cardW: int = 100; // Each card's width and height is 100 pixels
var cardH: int = 100;
var aCards: Array; // We'll store all the cards we create in this Array
var aGrid: Array; // This array will keep track of the shuffled, dealt cards
var aCardsFlipped: ArrayList; // This array will store the two cards that the player flips over
var playerCanClick: boolean; // We'll use this flag to prevent the player from clicking buttons when we don't want him to
var playerHasWon: boolean = false; // Store whether or not the player has won. This should probably start out false :)
function Start() {
    playerCanClick = true; // We should let the player play, don't you think?
    // Initialize the arrays as empty lists:
    aCards = new Array();
    aGrid = new Array();
    aCardsFlipped = new ArrayList();
    BuildDeck();
    for (i = 0; i < rows; i++) {
        aGrid[i] = new Array(); // Create a new, empty array at index i
        for (j = 0; j < cols; j++) {
            var someNum: int = Random.Range(0, aCards.length);
            aGrid[i][j] = aCards[someNum];
            aCards.RemoveAt(someNum);
        }
    }
}

function OnGUI() {
    GUILayout.BeginArea(Rect(0, 0, Screen.width, Screen.height));
    BuildGrid();
    GUILayout.EndArea();
    print("building grid");
}

function BuildGrid() {
    GUILayout.BeginVertical();
    GUILayout.FlexibleSpace();
    for (i = 0; i < rows; i++) {
        GUILayout.BeginHorizontal();
        GUILayout.FlexibleSpace();
        for (j = 0; j < cols; j++) {
            var card: Object = aGrid[i][j];
            var img: String;
            if (card.isFaceUp) {
                img = card.img;
            } else {
                img = "wrench";
            }
            if (GUILayout.Button(Resources.Load(img),
                    GUILayout.Width(cardW))) {
                if (playerCanClick) {
                    FlipCardFaceUp(card);
                }
                Debug.Log(card.img);
            }
        }
        GUILayout.FlexibleSpace();
        GUILayout.EndHorizontal();
    }
    GUILayout.FlexibleSpace();
    GUILayout.EndVertical();
}
class Card extends System.Object {
    var isFaceUp: boolean = false;
    var isMatched: boolean = false;
    var img: String;

    function Card(img: String) {
        this.img = img;
    }
}

function BuildDeck() {
    var totalRobots: int = 4; // we've got four robots to work with
    var card: Object; // this stores a reference to a card
    for (i = 0; i < totalRobots; i++) {
        var aRobotParts: Array = ["Head", "Arm", "Leg"];
        for (j = 0; j < 2; j++) {
            var someNum: int = Random.Range(0, aRobotParts.length);
            var theMissingPart: String = aRobotParts[someNum];
            aRobotParts.RemoveAt(someNum);
            card = new Card("robot" + (i + 1) + "Missing" + theMissingPart);
            aCards.Add(card);
            card = new Card("robot" + (i + 1) + theMissingPart);
            aCards.Add(card);
        }
    }
}

function FlipCardFaceUp(card:Card)
{
card.isFaceUp = true;
if(aCardsFlipped.IndexOf(card) < 0)
{
aCardsFlipped.Add(card);
if(aCardsFlipped.Count == 2)
{
playerCanClick = false;
yield WaitForSeconds(1);
aCardsFlipped[0].isFaceUp = false;
aCardsFlipped[1].isFaceUp = false;
aCardsFlipped = new ArrayList();
playerCanClick = true;
}
}
}