Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
35 views

COMP3329 Tutorial5

This tutorial teaches how to create a basic 3D basketball game in Unity. It includes instructions on importing assets, configuring the hoop, basketball, background, physics, and a script to track score. When the user presses a button, the basketball is thrown based on an energy bar that determines force. If it enters the hoop trigger, the score increases. The completed game allows throwing a basketball into a hoop to score points.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

COMP3329 Tutorial5

This tutorial teaches how to create a basic 3D basketball game in Unity. It includes instructions on importing assets, configuring the hoop, basketball, background, physics, and a script to track score. When the user presses a button, the basketball is thrown based on an energy bar that determines force. If it enters the hoop trigger, the score increases. The completed game allows throwing a basketball into a hoop to score points.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Tutorial 5.

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

Test the game!


11
Setting Background
Choose “Game Object” → “3D Object” → “Plane”
Rename the object as “BG” and update its
properties in the “Inspector” window as follows:
Position: 0, 0, 37.2
Rotation: 90, 180, 0
Scale: 14, 7, 7
Select “BG” and drag “bg.jpg” into the “Inspector”
window.

12
Setting Background

Test the game!

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.

Test the game!

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.

Test the game!


17
Adding Track Point
Choose “Game Object” → “3D Object” and “Cube”
Rename the object as “PointEvent” and change its
properties in the “Inspector” window as follows:
Position: -0.12, 0.53, 0
Rotation: 0, 0, 0
Scale: 1, 1, 1
This cube is for detecting whether the basketball enters
the hoop and so please check “Is Trigger”.
This cube should not be seen by players and so please
uncheck “Mesh Renderer”.
18
Adding Track Point

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;

public class score : MonoBehaviour {

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.

if (Input.GetButtonDown("Fire1")) { “Fire1” ➔ “Ctrl” key


GetComponent<Rigidbody>().isKinematic = false;
GetComponent<Rigidbody>().AddForce (Vector3.up * (vSbarValue*120) );
GetComponent<Rigidbody>().AddForce (Vector3.forward * 180);
GetComponent<Rigidbody>().AddForce (Vector3.right * 2);
}
if(gameObject.gameObject.transform.position.y < -10) {
GetComponent<Rigidbody>().velocity = new Vector3(0, 0, 0);
GetComponent<Rigidbody>().isKinematic = true;
GetComponent<Rigidbody>().position=new Vector3(0, -0.83f, -6.56f);
}
}
23
score.cs
void OnTriggerExit(Collider other) {
if (other.transform.name == "PointEvent") {
mScore=mScore+1;
}
}
void OnGUI () {
GUI.VerticalScrollbar(new Rect(25, 20, 100, 120),
vSbarValue, 1.0F, 10.0F, 0.0F);
GUI.Label(new Rect(0,0,400,400)," Score:"+mScore);
}
}
24
Sample Output

The game is far from perfect. Please feel free to enhance it


in any way. 25
Save and Submit Your Work
Please save your work, zip the project folder and
submit to Moodle by April 16, 2020 (Thursday) as
a proof of lab participation.

26
We are with you!

If you encounter any problems in understanding the materials


in the lectures, please feel free to contact me or my TA.
We are always with you!

We wish you enjoy learning game programming in this class.


27
Tutorial 5.

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

You might also like