5 - User Interface Flashcards

In this Mission, you will program a game to test the player’s reflexes, where the goal is to click and destroy objects randomly tossed in the air before they can fall off the screen. In creating this prototype, you will learn how to implement a User Interface - or UI - into your projects. You will add a title screen with a difficulty select menu that will control how challenging the gameplay is, you will add a score display that will track how many points the player has earned, and you will ad (42 cards)

1
Q

4.1.3 - Toss objects randomly into the air

What is AddForce.Torque

A

apply a rotational force to a Rigidbody component, causing it to rotate around its center of mass.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

4.1.3 - Toss objects randomly into the air

What is the syntax for AddForce.Torque?

A

AddForce.Torque(Vector3 torque)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

4.1.3 - Toss objects randomly into the air

We need to toss four prefabs into the air with a random force, torque, and position. We also want an immediate force, so use the proper ForceMode.

In your “Target” script, write the statements.

A

private Rigidbody targetRb;

void Start()
{
targetRb = GetComponent< Rigidbody >();
targetRb.AddForce(Vector3.Up * Random.Range(12, 16), ForceMode.Impulse);
targetRb.AddTorque(Random.Range(-10,10), Random.Range(-10,10), Random.Range(-10,10), ForceMode.Impulse);

transform.position = new Vector3(Random.Range(-4,4), -6);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

4.1.3 - Toss objects randomly into the air

We need to toss four prefabs into the air with a random force, torque, and position. Describe what you will need to do in the Target script to achieve this.

A
  1. Call the Target rigidbody
  2. Add randomized upward force
  3. Add randomized rotation torque
  4. Randomize the X position
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

4.1.4 - Replace messy code with new methods.

We’re going to store our random force, torque and position in brand new clearly named custom methods.
1. Write new variables min/max speed, spawn positions, min/max torque. HINT: Simplify.
2. Update the code
3. Write new methods for RandomForce, RandomTorque, RandomSpawnPos.

private Rigidbody targetRb;

void Start()
{
targetRb = GetComponent< Rigidbody >();
targetRb.AddForce(Vector3.Up * Random.Range(12, 16), ForceMode.Impulse);
targetRb.AddTorque(Random.Range(-10,10), Random.Range(-10,10), Random.Range(-10,10), ForceMode.Impulse);

transform.position = new Vector3(Random.Range(-4,4), -6);
}

Tip: Don’t forget that some methods must return a value

A

private Rigidbody targetRb;

private float minSpeed = 12;
private float maxSpeed = 16;
private float maxTorque = 10;
private float spawnPosY = -6;
private float spawnPosX = 4;

  
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
  targetRb = GetComponent< Rigidbody >();  

  targetRb.AddForce(RandomForce(), ForceMode.Impulse);
  targetRb.AddTorque(RandomTorque(), RandomTorque(), RandomTorque(), ForceMode.Impulse);

  transform.position = RandomSpawnPos();
}
  

**Vector3 RandomForce()
{
    return Vector3.up * Random.Range(minSpeed, maxSpeed);
}

float RandomTorque()
{
  return Random.Range(-maxTorque, maxTorque);
}

Vector3 RandomSpawnPos()
{
   return new Vector3(Random.Range(-spawnPosX, spawnPosX), spawnPosY); 
} **
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

4.1.5 - Create an Object list in Game Manager

What is List<> ?

A

used to store a dynamic array of objects of a specific type T

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

4.1.5 - Create an Object list in Game Manager

What is the difference between a List and an Array?

A

With List we can pass in the type of thing that we want, with arrays we have to tell the array what the thing is before we make the array itself.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

4.1.5 - Create an Object list in Game Manager

What is the Syntax for List?

A

List<> stringname

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

4.1.5 - Create an Object list in Game Manager

Write a List to use for your Targets

A

public List< GameObject > targets;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

4.1.4 - Replace messy code with new methods.

We’re going to store our random force in a brand new clearly named custom methods.
1. Write new variables for min/max speed
2. Update the code
3. Write new method for RandomForce

private Rigidbody targetRb;

void Start()
{
targetRb = GetComponent< Rigidbody >();
targetRb.AddForce(Vector3.Up * Random.Range(12, 16), ForceMode.Impulse);
targetRb.AddTorque(Random.Range(-10,10), Random.Range(-10,10), Random.Range(-10,10), ForceMode.Impulse);

transform.position = new Vector3(Random.Range(-4,4), -6);
}

A

private Rigidbody targetRb;
private float minSpeed = 12;
private float maxSpeed = 16;

void Start()
{
targetRb = GetComponent< Rigidbody >();

targetRb.AddForce(RandomForce(), ForceMode.Impulse);
targetRb.AddTorque(Random.Range(-10,10), Random.Range(-10,10), Random.Range(-10,10), ForceMode.Impulse);

transform.position = new Vector3(Random.Range(-4,4), -6);
}

Vector3 RandomForce()
{
return Vector3.up * Random.Range(minSpeed, maxSpeed);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

4.1.4 - Replace messy code with new methods.

We’re going to store our random torque in a brand new custom method.
1. Write new variables for min/max torque
2. Update the code
3. Write new method for RandomTorque.

private Rigidbody targetRb;

void Start()
{
targetRb = GetComponent< Rigidbody >();
targetRb.AddForce(Vector3.Up * Random.Range(12, 16), ForceMode.Impulse);
targetRb.AddTorque(Random.Range(-10,10), Random.Range(-10,10), Random.Range(-10,10), ForceMode.Impulse);

transform.position = new Vector3(Random.Range(-4,4), -6);
}

A

private Rigidbody targetRb;
private float maxTorque = 10;

void Start()
{
targetRb = GetComponent< Rigidbody >();

targetRb.AddForce(Vector3.Up * Random.Range(12, 16), ForceMode.Impulse);
targetRb.AddTorque(RandomTorque(), RandomTorque(), RandomTorque(), ForceMode.Impulse);

transform.position = new Vector3(Random.Range(-4,4), -6);
}

float RandomTorque()
{
return Random.Range(-maxTorque, maxTorque);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

4.1.4 - Replace messy code with new methods.

We’re going to store our random position in a brand new custom method.
1. Write new variables for spawn positions
2. Update the code
3. Write new method for RandomSpawnPos.

private Rigidbody targetRb;

void Start()
{
targetRb = GetComponent< Rigidbody >();
targetRb.AddForce(Vector3.Up * Random.Range(12, 16), ForceMode.Impulse);
targetRb.AddTorque(Random.Range(-10,10), Random.Range(-10,10), Random.Range(-10,10), ForceMode.Impulse);

transform.position = new Vector3(Random.Range(-4,4), -6);
}

A

private Rigidbody targetRb;
private float spawnPosX = 4;
private float spawnPosY = -6;

void Start()
{
targetRb = GetComponent< Rigidbody >();
targetRb.AddForce(Vector3.Up * Random.Range(12, 16), ForceMode.Impulse);
targetRb.AddTorque(Random.Range(-10,10), Random.Range(-10,10), Random.Range(-10,10), ForceMode.Impulse);

transform.position = RandomSpawnPos();
}

** Vector3 RandomSpawnPos()
{
return new Vector3(Random.Range(-spawnPosX, spawnPosX), spawnPosY);
}**

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

4.1.6 - Create a coroutine to spawn objects

Now that we have a list of object prefabs, we should instantiate them in the game using coroutines and a new type of loop.

In your GameManager script, create a new Coroutine called SpawnTarget
2. Crete and include spawnRate in for seconds
3. Create an Index variable with a random range using your targets List
4. Instantiate the targets index
5. Write the Start co-routine statement

A

private float spawnRate = 1.0f;

void Start()
{
    **StartCoroutine( SpawnTarget () ); **
}

IIEnumerator SpawnTarget()
{
while(true)
{
yield return new WaitForSeconds(spawnRate);
int index = Random.Range(0, targets.Count);
Instantiate(targets[index]);
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

4.1.6 - Create a coroutine to spawn objects

When using List, we cannot use Length like we do an array. What do we use instead of Length?

A

Count

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

4.1.6 - Create a coroutine to spawn objects

In our SpawnTarget method, write the randomized index statement to fill in the code:

IEnumerator SpawnTarget()
{
while(true)
{
yield return new WaitForSeconds(spawnRate);
_________________________________________________
Instantiate(targets[ ]);
}
}

A

int index = Random.Range(0, targets.Count);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

4.1.6 - Create a coroutine to spawn objects

Now that we have most of our SpawnTarget code block written, Instantiate the targets List.

IEnumerator SpawnTarget()
{
while(true)
{
yield return new WaitForSeconds(spawnRate);
int index = Random.Range(0, targets.Count);
_________________________________________________
}
}

A

Instantiate(targets[index]);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Now that our targets are spawning and getting tossed into the air, we need a way for the player to destroy them with a click. We also need to destroy any targets that fall below the game view and trigger the game sensor.

In your Target script, write methods that destroy the game object when the mouse is clicked, and when the gameobject meets the collider of the object sitting below the camera view.

A

private void OnMouseDown()
{
Destroy(gameObject);
}

private void OnTriggerEnter(Collider other)
{
Destroy(gameObject);
}

18
Q

5.2.1 - Add Score text and position it on screen

We are going to begin adding text in our game. What is the path to TextMeshPro?

A

Hierarchy > Create button > UI > TextMeshPro

19
Q

5.2.1 - Add Score text and position it on screen

What is the name of the object all of our text and UI elements appear on?

20
Q

4.2.3 - Initialize score text and variable

what is the name of the library we need to use the different methods and statements with TextMeshPro?

21
Q

4.2.3 - Initialize score text and variable

We are going to write a variable to keep track of our score in the UI. What script should it go in?
1. Target
2. GameManager

22
Q

4.2.3 - Initialize score text and variable

Write the variable for which we cann the text we made, “scoreText”

A

public TextMeshProUGUI scoreText;

23
Q

4.2.3 - Initialize score text and variable

What is TextMeshProUGUI?

24
Q

4.2.3 - Initialize score text and variable

With our TextMeshUGUI scoreText called in for our UI, what other variable do we need in order to keeep track of a score?

A

private int score;

25
# 4.2.3 - Initialize score text and variable Set the score integer to zero at start, and write the statment for scoreText that will display and add the score
void Start() { StartCoroutine(SpawnTarget()); score = 0; scoreText.text = "Score: " + score; }
26
Create a new UpdateScore method that requires one int scoreToAdd parameter
private void UpdateScore(int scoreToAdd){}
27
Cut and paste scoreText.text = "Score: " + score; into the new method UpdateScore(), then call UpdateScore(0) in Start() void Start() { StartCoroutine(SpawnTarget()); score = 0; scoreText.text = "Score: " + score; } private void UpdateScore(int scoreToAdd) { }
void Start() { StartCoroutine(SpawnTarget()); score = 0; **UpdateScore(0)**; } private void UpdateScore(int scoreToAdd) { **scoreText.text = "Score: " + score;** }
28
Have UpdateScore() increment the score by adding: void Start() { StartCoroutine(SpawnTarget()); score = 0; UpdateScore(0); } private void UpdateScore(int scoreToAdd) { scoreText.text = "Score: " + score; }
void Start() { StartCoroutine(SpawnTarget()); score = 0; UpdateScore(0); } private void UpdateScore(int scoreToAdd) { ** score += scoreToAdd;** scoreText.text = "Score: " + score; }
29
To test the increment score is working in UpdateScore(), make the GameManager add 5 points for each gameObject that spawns in the scene. void Start() { StartCoroutine(SpawnTarget()); score = 0; UpdateScore(0); } private void UpdateScore(int scoreToAdd) { score += scoreToAdd; scoreText.text = "Score: " + score; } IEnumerator SpawnTarget() { while(true) { yield return new WaitForSeconds(spawnRate); int index = Random.Range(0, targets.Count); Instantiate(targets[index]); } }
void Start() { StartCoroutine(SpawnTarget()); score = 0; UpdateScore(0); } private void UpdateScore(int scoreToAdd) { score += scoreToAdd; scoreText.text = "Score: " + score; } IEnumerator SpawnTarget() { while(true) { yield return new WaitForSeconds(spawnRate); int index = Random.Range(0, targets.Count); Instantiate(targets[index]); **UpdateScore(5);** } }
30
What is score += scoreToAdd saying/meaning?
It is adding the scoreToAdd to the score.
31
Describe the score mechanics at play in your own words. What does each variable do, how do they connect, etc. void Start() { StartCoroutine(SpawnTarget()); score = 0; UpdateScore(0); } private void UpdateScore(int scoreToAdd) { score += scoreToAdd; scoreText.text = "Score: " + score; } IEnumerator SpawnTarget() { while(true) { UpdateScore(5); } }
In the start method we set the score to be zero, and we see UpdateScore(int) being zero (0) as well. In the UpdateScore method we write, (int scoreToAdd) is placed inside parentheses - we are telling the Method to hold the place within () as an int. This means when we call UpdateScore() in other code blocks, we will be able to tell it the exact amount to weigh. With score +=, it is saying the score will be equal to PLUS whatever scoreToAdd is. Again in different code blocks these would be different values within the method () that get added to the score. scoreText.text is the UI element that holds both the text and concatenation with the score. In SpawnTarget(), basically UpdateScore holds a value of (5) every time a game object is spawn.
32
# 5.2.5 - Add score when targets are destroyed What would happen if UpdateScore(0) in the Start method was not there?
The Score UI will show no integer whatsoever. It will be blank. This could easily be a design choice, but by setting it to (0) it at least shows that the score is in fact there.
33
# 5.2.5 - Add score when targets are destroyed What would happen is scoreText.text = "Score: " + score was not in the UpdateScore() method, but perhaps still in the Start block?
34
# 5.2.5 - Add score when targets are destroyed Now that we have a method to update the score, we should call it in the target script whenever a target is destroyed. In your Target script, how would you write the variable to call in the GameManager script?
public GameManager gameManager;
35
# 5.2.5 - Add score when targets are destroyed Now that we have a method to update the score, we should call it in the target script whenever a target is destroyed. Using your gameManager variable, call the GameManager script in the Start method of the Target script.
void Start() { gameManager = GameObject.Find("Game Manager").GetComponent< GameManager >; }
36
# 5.2.5 - Add score when targets are destroyed Now that we have a method to update the score in the Game Manager script, we should call it in the Target script whenever a target is destroyed. To find the game manager script from within Target, what do we need to find and why?
We need to find the GameObject "Game Manager" so that we can get the component of the script itself, and ultimately call the UpdateScore method from it in Target.
37
# 5.2.5 - Add score when targets are destroyed Now that we have a method to update the score, we should call it in the target script whenever a target is destroyed. Update the Target script to score any integer of your choosing when an object is clicked.
private void OnMouseDown() { ...gameManager.UpdateScore(5); }
38
# 5.2.6 - Assign a point value to each target The score gets updated when targets are clicked, but we want to give each of the targets a different value. In Target script, create a new public variable to assign points to each prefab (in the inspector). Then, update the script to store the scoring variable when an object is clicked.
public int pointValue; private void OnMouseDown() { ...gameManager.UpdateScore(pointValue); }
39
# 5.2.6 - Assign a point value to each target The score gets updated when targets are clicked, but we want to give each of the targets a different value. Currently everything is worth 5 - what can we do to assign each prefab a different value?
Create a new public int value that can be assigned a different value to each prefab in the inspector. Then update the UpdateScore code block in OnMouseDown with the variable.
40
# 5.2.7 - Add a particle explosion What is the variable method needed for a particle?
ParticleSystem
41
# 5.2.7 - Add a particle explosion Write a variable to assign an explosion particle in Target
public ParticleSystem explosionparticle;
42
# 5.2.7 - Add a particle explosion We have assigned explosion particles to each prefab. Where should you instantiate the particles? Write the code block.
void OnMouseDown() { ... Instantiate(explosionParticle, transform.position, explosionParticle.transform.rotation); }