Tuesday 2 December 2014

Games Encounters Ewan Armstrong Session 4

Today we had the goal of "cleaning" our script for the door-move by making our code D.R.Y (Don't Repeat Yourself). This is the action of not repeating lines of code and only requiring one line of the code you need which can be called upon by a simple line of code. 


Here we made a void function followed by our own title DoorMotion (this has no significance other than the name of the function we want our doors to have) added () to the end and placed an open and close {} bracket with a line between them. To test it we created a Debug.Log within the {} brackets, followed by ("enter text here"); I put the word Flujible in the brackets. We then moved the If and Else of the door-move script already written in (shown in the image above) down into our new Void DoorMotion code below the Debug but within the {} brackets.

We then added Vector3 endPos1, endPos2 within the () brackets next to void DoorMotion

Then on this line of code:
myDoor1.transform.localPosition = Vector3.Lerp(myDoor1.transform.localPosition,new Vector3 (doormove, 0f, 0f), moveSpeed * Time.deltaTime));
and this:
myDoor1.transform.localPosition = new Vector3 (doormove, 0f, 0f;

We changed the new Vector3 (doormove, 0f, 0f line of code to endPos1 (and endPos2 for the second line of both)

The difference can be seen between the image above and below.


Now back in the position where the IF and Else code used to be I added the line of code - DoorMotion(new Vector3(doorMove, 0f, 0f), new Vector3(-doorMove, 0f, 0f));

Now in the void Update section when this code is called up it will take it from our own personal code we made in void DoorMotion.




Now in the else IF section below our new line of code in the void Update section we added the code:

DoorMotion(Vector3.zero, Vector3.zero);

Now in the else IF section when the DoorMotion code is called and is false, it will call up the line of code above to move the doors back to their original local position. This cleans up our code leaving just the one section which contains a lot of text for the door function but has been shortened into these small lines of code in the call up section - void Update

Next we moved onto applying GUI Text to an object which once moused over the text will appear.

We began by making the GUIText as a game object.


We then added a c# script component to the object which shall have the text attached to appear once moused over. In the script we added a public variable to attach the GUIText game object to in the script.


Now we added a new void called OnMouseOver (as we only want the text to appear once we hove our mouse over the object.), adding a () bracket on the end and {} brackets below with a line separating them.
In this line between the {} brackets we typed in the code:
KeyHover.text = "I am a Key";
now the code is connected to the public variable and the .text feature allows the GUIText to appear once the function is called. The = gives the code a value (a value being that actual thing the code is producing, in this case a text pop-up). At the end we add a semi-colon to finish the line of code. Now when we run the game and mouse over the game object the text within the "" appears but does not go away so we moved on to fix that.


Now we added a new void called OnMouseExit() again adding the {} brackets and in the space between them added the line of code:
KeyHover.text = null; 
Now when our mouse is no longer on the game object the GUIText disappears.


Now we want to make the function that makes the game object which is our key disappear when we click on it. For this we created a new void to be called up called void OnMouseDown() with the usual {} brackets. In their space we typed up the code:
StartCoroutine("KeyPickUp");
With IEnumerator KeyPickUp() set up beneath. This starts a function but allows said function to yield it's end result without hogging the CPU.
Now we could write code to disable the renderer and collider of the key game object and have the text "I have the key" appear on mouse down. This code is as follows:
Debug.Log ("Flujible");

KeyPick.text = "I have the key";

renderer.enabled = false;

collider.enabled = false;

yield return new WaitForSeconds (3f);

KeyPick.text = null;

The renderer and collider code switches off the box cllider and mesh renderer on the key game object, the KeyPick.text creates GUIText once the game object has been clicked and the yield return code allows a delay between the text appearing on mouse down and disappearing. However because it is a wait function and requires a yield the code expects a return function so we added the KeyPick.text = null; code which satisfies the code and switches off the text after the set time.




No comments:

Post a Comment