Sunday, 20 November 2016

Week 3


 In the third week, we were introduced in C#, and in Unity. The outcome of that week was to get used with the C# scripts in Unity and to have a playable demo of a game featuring some elements like: moving non-player characters, a player that can be controlled with a keyboard and a mouse, gravity or other external forces and the display of the score on the GUI.

 The following code illustrates the movement of the main character which is controlled by the keyboard (replacing the accelerometer) and the mouse (as a substitute for the touch controls). The "if statements" are written for every arrow in order to allow the player to move the character in every desired position in order to collect the points:






 The succeeding script is for the NPC behaviour. The "transform.Translate(new Vector3(0, 1, 0) * Time.deltaTime  moveSpeed)" allows the NPC to move along the Y axis (up), moves the sprites in meters per second, and if the user wants to try different speeds the "moveSpeed" variable can be added:






 The code for the movement of the item (the one which the player needs to collect in order to accumulate points) is similar with NPC's script, but without the translate statement in the Start method: 




 To make the character interact with the other moving game object and make a certain in-game object to disappear we need to add a Box Collider to our main character and to create a new script where we need to update the Update method with a new method. The following picture will illustrate, as well, the code for displaying the score on the GUI, at the beginning of script we need to add a class variable, which needs to be private, and we need to set the initial value to 0:




 The last script is used for the camera, which is supposed to follow the player. At the start of the script we need to set some variables, the offset variable is private because we can set the values in the script. For the offset value, we can take the current transform position of the camera and subtract it with the transform position of the main character to find out the difference between the two values. In the Start method, we will declare offset value being equal to the difference between the two positions, of the screen and character: 







No comments:

Post a Comment