Home Page > > Details

Ghostwriter CST8237 Programming,Help With C/C++ Programming,c++ ProgrammingDebug Help With Java Programming|Debug Matlab Programming

CST8237: Games Development
Lab 2: Roll a Ball (cont.)
13 January 2017
Part II
1 Position and Velocity
In this second part we are going to use some of the concepts learnt in the last lecture. First of all, we are going
to add some debug information to our GUI canvas. Create a new text object (PlayerPosition), and place it in
the bottom left corner of the screen. Text alignment must be Left, font color Black and font size 16.
Now, following the same procedure as in the first part, print in this text the player’s position (taken from
the player’s transform) at every step. Note that you can format the float values of the position vector with the
C# function ToString("0.00").
Repeat the process indicating the velocity (as a vector, with its coordinates) of the player as well. Note that
for a new text you also need to modify the position of the Rect Transform component. The easiest way to get
the velocity of an object in Unity is to directly read rigidbody.velocity. However, this assumes the object
has a rigidbody, which is not necessary. Instead, we’re going to use vector math for this.
The velocity can be calculated as the difference between the current and the last position of the object,
divided by the time elapsed (Time.deltaTime). Calculate this velocity and print it on the screen over the text
with the position of the player.
2 Code organization
The code in PlayerController class, if you’ve been following these instructions, is starting to get quite messy.
Certainly, all things related to GUI or the game state (score, or victory conditions) shouldn’t be in the player’s
controller. Code organization and modularity is essential for maintenance of large project, and game programming
is not an exception.
A good practice is to have a separated object for this. Create a new empty object, give it a nice name (GameController,
for instance) and create a C# script for it. Move all the following objects from the PlayerController
class to GameController :
• All text fields.
• The functions that update these text should be moved and made public.
• Variables for the number of pickups and the count of collected ones.
• As for the GUI elements, create public functions in GameController that update the game state variables.
GameController and PlayerController are two objects that need to communicate with each other. GameController
needs to ask the player for its position and velocity, while PlayerController must indicate the GameController
when a pickup has been collected. We’ll set up this two-way communication using two different
methods:
1
• The GameController should have now a reference to the player. This can be exposed as a public variable
in PlayerController and set from the editor.
• The PlayerController will hold a (private) reference to the GameController, which is initialized in the
Start() method. Set the tag “GameController” to the GameController object in the editor, and use the
functions ameObject.FindGameObjectWithTag and GetComponent<>() to set up this reference in the code
of PlayerController.
Also, for some features needed further on, we are going to give the game controller access to the pickup
objects. Create an array of GameObject in the GameController class, and assign these objects in the editor.
3 Back to the Math
Probably you have noticed, but velocity as a vector is not very human readable. Modify the code so the text
also reflects the speed as a scalar. Note that you have to get this value from the player’s velocity, instead of
printing the value of the variable speed. Why are these different? What are the units of this speed?
The next task is to add another text label that indicates the distance to the closest pickup. Place this over
the player velocity text, and add the necessary code to do so in the GameController script. Remember to take
into account those that have been already collected. In order to highlight which is the closest pickup, change
its material colour to blue with the following line:
1 pickup [ i ]. GetComponent < Renderer >() . material . color = Color . blue ;
Remember that you must also set the other colours back to white:
1 pickup [ i ]. GetComponent < Renderer >() . material . color = Color . white ;
Let’s add now some graphical debug indicators. Unity provides a class (LineRenderer) that allows to...
render lines in the game. Include the following lines in the variable section and the Start() function of the
GameController class:
1 public class GameController : MonoBehaviour {
2 // ...
3 private LineRenderer lineRenderer ;
4 void Start () {
5 // ...
6 lineRenderer = gameObject . AddComponent < LineRenderer >() ;
7 }
8 // ...
9 }
To draw a line with a LineRenderer, you must call (at least) the following three functions:
1 // 0 for the start point , position vector ’ startPosition ’
2 lineRenderer . SetPosition (0 , startPosition ) ;
3 // 1 for the end point , position vector ’endPosition ’
4 lineRenderer . SetPosition (1 , endPosition ) ;
5 // Width of 0.1 f both at origin and end of the line
6 lineRenderer . SetWidth (0.1 f , 0.1 f ) ;
Render one of these lines from the player to the closest pickup identified previously.
4 Debug Modes
The next step is to define different execution modes. The idea is to have the following status of the game,
regarding the debug information:
• Normal: No debug information is shown.
• Distance: Player position and velocity are shown, including the distance to the closest pickup and the
line from the player to this object. All pick-ups are white, with the exception of the closest one, which is
rendered in blue (this is the state the game should be at the moment).
2
• Vision: Described next.
In order to switch from one state to the next, capture the input of the key Space, and switch from one to
the next (circularly). Suggestion: use a C# enum type to code this.
For the Vision mode, the following features must be implemented:
• The line renderer must draw a line that shows the current velocity of the player. The starting point should
be the position of the player, while the ending point is this position plus its velocity.
• All pick-ups must be white, with the exception of that one that the player is approaching more directly,
which should be rendered green. This is, using the velocity of the player, determine which pick-up is more
likely to be collected next (even if following the current direction wouldn’t actually collide with anyone).
Think which vector operation you need to determine this.
• The green pick-up must be oriented towards the player, instead of performing the perpetual orientation
that they are usually in. Use the function LookAt for this feature. All other white pick-ups (even if they’ve
turned green in the past) must keep rotating as before.
5 Building the game
When the game is finished, you can build it going to File → Build & Run. Select the platform you want to build
the game for and the target’s attributes on the right. Then, click on Add Current to add the current scene to
the build and press Build. This should prompt you with an output folder to save the standalone built, that you
can run from outside Unity. Select a destination folder and click on Save (this could take some time for larger
projects, but just a few seconds for this one).
For this assignment, generate a build for the Web Browser and test it opening the html file generated by
Unity. You should be able to play the game in the browser, and it should look and behave exactly as in the
Unity editor when you play-test it.
3

Contact Us - Email:99515681@qq.com    WeChat:codinghelp
© 2014 www.asgnhelp.com
Programming Assignment Help!