COMP3329 Tutorial5
COMP3329 Tutorial5
3D Basketball Game
2019-2020
COMP3329 Computer Game Design and Programming
Dr. T.W. Chim (E-mail: twchim@cs.hku.hk)
Department of Computer Science, The University of Hong Kong
What are we going to do?
A basketball game including a hoop, a basketball
and a background.
An energy bar showing a fluctuating energy level
which determines the force applied to the
basketball.
When clicking anywhere of the screen, the
basketball will be thrown into the hoop based on
the amount of force applied.
2
Preparation
Download resources5.zip from Moodle and unzip
it. You now have the following in a folder named
"resources5".
basketball.obj
basketball-texture.jpg
bg.jpg
hoop.fbx
Score.cs
3
Setup Unity
Open Unity, and in the top menu do “File -> New
Project”. Set your project directory, and make
sure to select “3D” in the dropdown.
Go to the Assets window and create a folder
“Models”. Drag the files in the “resources5”
folder into it.
4
Adding Hoop
Drag the 3D model “hoop” into the “Hierarchy” window.
Select it in the “Hierarchy” window and set its properties
in the “Inspector” view as follows.
Position: 0, 3.45, 0
Rotation: 270, 180, 0
Scale: 0.01, 0.01, 0.01
5
Adding Hoop
6
Adding Basketball
Drag the 3D model “basketball” into the “Hierarchy”
window.
Select it in the “Hierarchy” window and set its properties
in the “Inspector” view as follows.
Position: 0, 0, 0
Rotation: 0, 0, 0
Scale: 0.005, 0.005, 0.005
7
Adding Basketball
8
Setting Basketball’s Material
Since the basketball object does not contain any
material, it is currently white in color.
Select “basketball-texture.jpg” in the “Assets” window
and set its properties in the “Inspector” view as follows.
Max Size: 256
Format: RGB 24 bit
Click “Apply”
9
Setting Basketball’s Material
Select the basketball in the scene (you might need to zoom to
enlarge it) or select “basketball → basketball” in the “Hierarchy”
window
Drag “basketball-texture.jpg” in the “Assets” window to it
10
Setting Basketball’s Material
In the “Inspector” window, update the properties of “Materials”
by setting “Smoothness” to 0 and “Tiling” to 0.05, 0.05
Then save the project and save the scene
12
Setting Background
13
Configuring Game Physics
Select the basketball in the “Hierarchy” window
In its “Inspector” window, add the “Physics →
Rigidbody” component.
Uncheck “Is Kinematic”.
When you test the game, you will see that the
basketball drops due to gravity.
14
Configuring Game Physics
Select the basketball in the “Hierarchy” window
In its “Inspector” window, add the “Physics →
Sphere Collider” component
Set its Radius to 100
15
Configuring Game Physics
Select the hoop in the “Hierarchy” window
In its “Inspector” window, add the “Physics →
Mesh Collider” component.
16
Configuring Game Physics
Change the basketball’s position to (1, 10, 0) and
test the game.
19
Adding Script
Select the basketball
Drag “score.cs” into its “Inspector” window
Open “score.cs” using Visual Studio 2017 and
study what is going on
20
score.cs
using UnityEngine;
using System.Collections;
int mScore=0;
float vSbarValue = 0.0f;
void Start () {
GetComponent<Rigidbody>().velocity = new Vector3(0, 0, 0);
GetComponent<Rigidbody>().isKinematic = true;
}
21
score.cs
int t_valueUpDown = 0;
void Update () {
float translation = Time.deltaTime * 20;
if (vSbarValue > 9.0f) {t_valueUpDown = 1; }
if (vSbarValue < 0.0f) {t_valueUpDown = 0;}
if (t_valueUpDown == 0) {
vSbarValue = vSbarValue + translation;
} else {
vSbarValue = vSbarValue - translation;
}
22
GetButtonDown(buttonName) -
returns true during the frame the
score.cs user pressed down the virtual
button identified by buttonName.
26
We are with you!
End
2019-2020
COMP3329 Computer Game Design and Programming
Dr. T.W. Chim (E-mail: twchim@cs.hku.hk)
Department of Computer Science, The University of Hong Kong