Monday, July 28, 2014

Learning Unity and C#

Post no. 15


Friend and Tutor(s): David Tan - UNITY Game Developer at MediaSoft

Already having moderate knowledge of JavaScript and Actionscript, David was so kind to teach me UNITY and C#!

As for the finished lesson, we managed to create a game with two interchangeable scenes via clicking.

Here are a couple of screenshots!

This first screenshot here, shows how I could drag effects like water, smoke, particles and such from the assets.


He taught me how to animate via sprite!


For the Animation Script,
He used DeltaTime to Average out the time, basically giving it optimum time the graphics need. The sprite is easily separated, all I needed to do was go to Sprite Mode>check multiple> and slice. I have to make sure the background is transparent.

There was a problem at first when he was trying to animate it because the code kept waiting for the 8th Frame when it only went to the 7th. So, he added a -1 to its code like this:

(frame > animate.Length - 1) then  frame=0.

else,

GetComponent<SpriteRenderer> ().sprite = animate [(int)frame]; 

I needed to convert the integers into frames so the program can recognize them!

Then it worked fine!


Okay so basically this is what I've learned:

There's three types of coding that I could do, Boo Script, Java Script and C#. Personally I think C# is awesome cause they have drop down shortcuts for me to use like Actionscript. It's very much like the combination of the two; (JS and AS).

So Hierarchy is something like a library, - a place where you could temporary store your elements. To keep the area clean, it's good practice to make a Prefab folder where it could store all the elements' properties and children.

In the script, you could just load them up from the PreFab folder.

Individual scripts can be dragged and dropped into different elements. I could do it the old fashion way by giving it a tagname (kinda like instance name : FindObjectWithTag) but it would be too much of a hassle when I could just drag and drop!

So the Inspector contains all the element's properties. Like the scale, positions and such.
It's basically 3D space, so if I were to create a background, I would just need to insert a plane element onto the stage.

I could either choose Perspective (3D) or Orthographic (2D)

The clipping planes set the maximum and minimum for the camera.

To add a layer, I could just simply look for the through the first sentence in the Inspector tab. I could hide and see them just like I would do in AI or Photoshop.

Unity already has automatic physics ready at my dispense, so he dragged RigidBody into one of those elements. When he clicked play, it fell onto the plane(floor) created!
To access RigidBody, I would just need to go to top tool bar and hit 'Components' > 'Physics'.

I could create spotlights in hierarchy as well to play with shadows. (Shader)

Then, he opened a C# doc, and changed to position with code.
LocalPosition affects the scene space whereas Position affects just the object itself.

As for scaling, there is just LocalScale.

Float is for values that contain decimals whereas Int are for rounded of integers.

Just a basic little reminder:

up : +y
left : -x
right : +x
down : -y

To test script, like Trace in AS I was required to type 'Debug.Log("CALL/whateverIwanttocheck")

COLLIDING: The element falls and stays on top another element.
TRIGGER: The element can fall through another element. To enable this, click the trigger option at Box Collider.

If I see and error with Null Object, it means that one of the obj has no reference. Like, it;s calling something that isn't there. So it's best to check if you properly linked the objects to the elements.

To CLONE and object: "Instantiate(clone)" Instantiate basically means create, or initiate.

He put in a code Quatermion.identity and said it's basically for rotation.

Okay so basically for my game, I didn't want any key to be specific and have their own layers to upload. I want it at a first come first serve basis. So it should look like this:
It's put under Update so it keeps running.

void Update () {
if (Input.anyKey)
{
if (Input.inputString != "")
{
Debug.Log(Input.inputString + " call");
}


And at the top, to test :
string name = "dog";

Once played, the program recognized that 3 characters are present.

MOUSECLICK
To create a button, he first showed me the GUI method. Which is inefficient because it would keep running as it its placed under Update.

void OnGUI()
{
GUI.Label (new Rect (10, 10, 100, 100), "Blah!");

if (GUI.Button (new Rect (10, 50, 100, 100), "Channge", style)) {

Application.LoadLevel (1);
}
}

The Application.LoadLevel is to call the second scene. Once I publish, I could see its ID on the right to the name of the file. Like 0,1,2 and so on.

So for a more efficient way, he did this:

if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 100))
Debug.Log(hit.collider.name);
}

For the number beside GetMouseButtonDown(0):
0 : Left click on the mouse
1: Scroller on the mouse
2: Right Click on the mouse

Basically there's this RaycastHit function where a ray would be shot in front of the object to test whether it is there after the mouse is clicked. (therefore, it would only run one time) So, it could sense that it is there.

Ellipsoid is a kind of particle property.

To add text into the stage: GameObject>insert 3D Text)

TO EXPORT: I can go to File>Build Settings and click switch platform if I needed to switch between mobiles and what not.

TO HIDE A SCENE/ELEMENT: It's pretty straight forward, it's Other.GameObject.SetActive(false).

TO UPLOAD A FONT: Just drag and drop the ttf into the folders! 

For uploading the CAMERA : I just needed to paste this chunk of code.

WebCamTexture webcamTexture = new WebCamTexture();
renderer.material.mainTexture = webcamTexture;
webcamTexture.Play();

ENUM (Enumeration)
This term was new! But it works the same as SwitchCase in AS. I could put in switches in it too.

Basically the wonderfulness of c# is that you don't need to define every single element individually by creating public vars one by one. Which is annoying. I could just do this!! Just commas! Amazing!

public enum item
{
rock,
stick,
paper

then

if (pick == item.rock)
{
}
else if (pick == item.stick)
{
}
else if (pick == item.paper)
{
}

IT'S SO CLEAN!

Okay, now to the complicated part.


DELEGATE
Now what is that?
Delegate is kinda like a remote control/mini robot to help you organize which script to call first. Sometimes there are two scripts, one running slower than the other. We need delegate so that the faster script doesn't run so fast when the slower one is not completed yet. Basically, to input hierarchy and to check it the script is being run or not.

So, I will have to put this line of code under the Start function in my ChangeScene script:
objDelegate.call += Trigger;

and put :

if (Input.GetKeyDown(KeyCode.Space))
{
if (call != null)
call (" dog");
}

in my Delegate script. I have to make sure I put using System; at the top to call the system from the library if not its not going to work!

Anyway, I learned alot through this 4 hours! Amazing! Maybe TOA should hire him to teach UNITY. (it's the FUTURE!)



No comments:

Post a Comment