首页
网站开发
桌面应用
管理软件
微信开发
App开发
嵌入式软件
工具软件
数据采集与分析
其他
首页
>
> 详细
辅导CST8237编程设计、讲解C/C++语言程序、c++编程调试 讲解Java程序|调试Matlab程序
项目预算:
开发周期:
发布时间:
要求地区:
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
软件开发、广告设计客服
QQ:99515681
邮箱:99515681@qq.com
工作时间:8:00-23:00
微信:codinghelp
热点项目
更多
代做ceng0013 design of a pro...
2024-11-13
代做mech4880 refrigeration a...
2024-11-13
代做mcd1350: media studies a...
2024-11-13
代写fint b338f (autumn 2024)...
2024-11-13
代做engd3000 design of tunab...
2024-11-13
代做n1611 financial economet...
2024-11-13
代做econ 2331: economic and ...
2024-11-13
代做cs770/870 assignment 8代...
2024-11-13
代写amath 481/581 autumn qua...
2024-11-13
代做ccc8013 the process of s...
2024-11-13
代写csit040 – modern comput...
2024-11-13
代写econ 2070: introduc2on t...
2024-11-13
代写cct260, project 2 person...
2024-11-13
热点标签
mktg2509
csci 2600
38170
lng302
csse3010
phas3226
77938
arch1162
engn4536/engn6536
acx5903
comp151101
phl245
cse12
comp9312
stat3016/6016
phas0038
comp2140
6qqmb312
xjco3011
rest0005
ematm0051
5qqmn219
lubs5062m
eee8155
cege0100
eap033
artd1109
mat246
etc3430
ecmm462
mis102
inft6800
ddes9903
comp6521
comp9517
comp3331/9331
comp4337
comp6008
comp9414
bu.231.790.81
man00150m
csb352h
math1041
eengm4100
isys1002
08
6057cem
mktg3504
mthm036
mtrx1701
mth3241
eeee3086
cmp-7038b
cmp-7000a
ints4010
econ2151
infs5710
fins5516
fin3309
fins5510
gsoe9340
math2007
math2036
soee5010
mark3088
infs3605
elec9714
comp2271
ma214
comp2211
infs3604
600426
sit254
acct3091
bbt405
msin0116
com107/com113
mark5826
sit120
comp9021
eco2101
eeen40700
cs253
ece3114
ecmm447
chns3000
math377
itd102
comp9444
comp(2041|9044)
econ0060
econ7230
mgt001371
ecs-323
cs6250
mgdi60012
mdia2012
comm221001
comm5000
ma1008
engl642
econ241
com333
math367
mis201
nbs-7041x
meek16104
econ2003
comm1190
mbas902
comp-1027
dpst1091
comp7315
eppd1033
m06
ee3025
msci231
bb113/bbs1063
fc709
comp3425
comp9417
econ42915
cb9101
math1102e
chme0017
fc307
mkt60104
5522usst
litr1-uc6201.200
ee1102
cosc2803
math39512
omp9727
int2067/int5051
bsb151
mgt253
fc021
babs2202
mis2002s
phya21
18-213
cege0012
mdia1002
math38032
mech5125
07
cisc102
mgx3110
cs240
11175
fin3020s
eco3420
ictten622
comp9727
cpt111
de114102d
mgm320h5s
bafi1019
math21112
efim20036
mn-3503
fins5568
110.807
bcpm000028
info6030
bma0092
bcpm0054
math20212
ce335
cs365
cenv6141
ftec5580
math2010
ec3450
comm1170
ecmt1010
csci-ua.0480-003
econ12-200
ib3960
ectb60h3f
cs247—assignment
tk3163
ics3u
ib3j80
comp20008
comp9334
eppd1063
acct2343
cct109
isys1055/3412
math350-real
math2014
eec180
stat141b
econ2101
msinm014/msing014/msing014b
fit2004
comp643
bu1002
cm2030
联系我们
- QQ: 9951568
© 2021
www.rj363.com
软件定制开发网!