2 - Basic Gameplay Flashcards

Gain familiarity with some of the most important programming and Unity concepts, including if-then statements, random value generation, arrays, collision detection, prefabs, and instantiation. (74 cards)

1
Q

2.2.1 - Make the projectile fly forwards

Write the code to make a projectile prefab move forward through the scene. Utilize the variable ‘speed’ as though it has been defined in the script.

A

transform.Translate(Vector3.forward * time.Deltatime * speed)

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

2.2.1 - Make the projectile fly forwards

What would happen if the code used to make a projectile prefab move through the scene only consisted of transform.Translate(Vector3.forward * time.Deltatime) - missing a speed variable

A

The code will still compile and execute, however the object would likely move imperceptibly slow. This is because without a speed variable it’s just being scaled by time.Deltatime, which is .16 seconds per frame(@ 60fps)

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

2.2.1 - Make the projectile fly forwards

What would happen if the code used to make a projectile prefab move through the scene only consisted of transform.Translate(Vector3.forward) - missing a speed variable and time.Deltatime

A

Vector3.forward is shorthand for (0,0,1), so without deltatime or a speed variable it would move the object forward 1 unit per second * fps along the z axis.

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

2.2.1 - Make the projectile fly forwards

Define the code for a variable we will use as speed for our projectile prefab. Value is your preference.

A

public float speed = 10.0f

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

2.2.1 - Make the projectile fly forwards

What is a Prefab?

A

An asset we can keep re-using anytime, anywhere and it will always have the same behavior

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

2.2.2 - Make the projectile into a prefab

What happens when you drag an object to your Prefab folder?

A

Unity will ask if you want to create new or create variant

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

2.2.2 - Make the projectile into a prefab

We want the projectile to spawn from the player itself, meaning the playerobject must have a reference to the prefab projectile. What is the reference we need to write in the PlayerController script?

A

public GameObject projectilePrefab

Gameobject is a unity class, projectilePrefab is the name we are giving the variable that will be viewable in the Inspector.

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

2.2.2 - Make the projectile into a prefab

We want the projectile to spawn from the player itself. We’ve written the public GameObject projectilePrefab reference into the PlayerController script. What should you do with the projectile prefab object now?

A

You want to drag the projectilePrefab from the folder to the component in the Inspector, on your PlayerObject.

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

2.2.3 - Test for spacebar press

We need to write code to press a button that launches the projectile. What script should you write the code in, in what section of script should it be written, and what type of statement do you use?

A

Since it will spawn from the PlayerContoller, you want to use that gameObjects script to write the code.

The code type you will use is an ‘if’ statement (IF button is pressed, then LAUNCH projectile)

Should be written in the Update section, as this updates every frame.

if( )
{

}

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

2.2.3 - Test for spacebar press

Write the code in the PlayerController script to launch projectile when the space bar is pressed down.

A

if (Input.GetKeydown(KeyCode.Space));

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

2.2.3 - Test for spacebar press

What does GetKey do?

A

GetKey makes Unity’s Input class check whether a specific key on the keyboard is being held down during the current frame.

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

2.2.3 - Test for spacebar press

What is the difference between GetKeyDown and GetKeyUp?

A

GetKeyDown checks for the frame the key is first pressed. GetKeyUp checks for the frame the key is released.

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

2.2.3 - Test for spacebar press

What does KeyCode do?

A

Provides a set of constants that are mapped to all the keys. Makes it easy for you to locate the key you want to use.

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

2.2.3 - Test for spacebar press

What is happening in this code?
if (Input.GetKeydown(KeyCode.Space));

A

The code checks if the Spacebar key was pressed down during the current frame.

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

2.2.4 - Launch projectile on spacebar press

You’ve written the if statement for Unity to check when there is a space bar press.
if(Input.GetKeyDown(KeyCode.Space)

Now, write the string within the brackets { } to launch the projectile when the space bar is pressed.

HINT: Remember that we want the projectile to spawn FROM the playerObject.

A

{
Instantiate(projectilePrefab, transform.position, projectilePrefab.transform.rotation);
}

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

2.2.4 - Launch projectile on spacebar press

What is Instantiate?

A

it’s another way of saying we are going to create an object, or in this case create copies of an object that already exists.

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

2.2.4 - Launch projetile on spacebar press

What is the code syntax for Instantiate?

A

Instantiate(Object original, Vector3 position, Quaternion rotation)

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

2.2.4 - Launch projectile on spacebar press

The generic code string for our if statement to launch the projectile is:
Instantiate(Object original, Vector3 position, Quaternion rotation)

Define what each part of the code is doing.

A

Instantiate - Creating copies of our object
Object original - this is the prefab we are instantiating (projectilePrefab in this example)
Vector3 position - The position at which we want our object to start at.
Quaternion rotation - It’s asking for the rotation is should be.

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

2.2.4 - Launch projectile on spacebar press

Our if statement code to launch the projectile is the following:

Instantiate(projectilePrefab, transform.position, projectilePrefab.transform.rotation);

Explain what each part corresponds to and why we are doing it.

A

1.Instantiate will create copies of our prefab
2.projectilePrefab is our Object original, and it is the prefab we will be creating copies of in Instantiation.
3. transform.position is the Vector3 position we want the object to start at. Because we are spawning the projectile on the playerObject, we are using transform.position.
4. This is the Quaternion rotation which is Unity asking what the rotation should be. Rotation is already set up in the prefab, and stored in it’s transform. So we reference the prefab, and then write transform.rotation.

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

2.2.5 - Make animals into prefabs

What do you need to do with the animal prefabs in order to get them to move forward, toward the player character?

A

Attach the same MoveForward script that the projectilePrefab uses to move to each of the animals. Preferably before moving it to the prefab folder.

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

2.2.6 - Destroy projectiles offscreen

Write code to destroy the projectile if it goes out of bounds on the Z axis

HINT: variable topBound

A

if(transform.position.z > topBound)
{
Destroy(gameObject);
}

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

2.2.6 - Destroy projectiles offscreen

Write a new variable to define topBound with a value of 30.

A

private float topBound = 30.0f

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

2.2.7 - destroy animals offscreen

Write a new variable lowerBound so we can destroy animals past a certain Z value

HINT: negative value

A

private float lowerBound = -10.0f

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

2.2.7 - destroy animals offscreen

In the if statement that describes what happens if a gameobject hits your topBound, write the ‘else’ statement for animals that hit your lowerBound.

A

else if (transform.position.z < lowerBound)
{
Destroy(gameObject);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
# 2.2.6 - Destroy projectiles offscreen 2.2.6 - If you're adding a new script to an existing prefab, what do you need to do to make sure the prefab has been updated to run the new script
Select Overrides in the Inspector and select Apply
26
# 2.2.7 - destroy animals offscreen What is an 'else if' statement?
Something you add on to the end of an if statement so you can fulfill unique situations.
27
# 2.3.1- Create a Spawn Manager What is an Array?
An Array in programming is something that you can use to store multiple objects or variables inside of
28
# 2.3.1 - Create a Spawn Manager What does [] mean?
An array
29
# 2.3.1 - Create a Spawn Manager We need to create an array for animal prefabs. Write the variable to store the array using the title "animalPrefabs".
public GameObject [] animalPrefabs;
30
# 2.3.1 - Create a Spawn Manager Why would we create an empty game object to attach our SpawnManager script to?
So that we can create the public variable array for use in the Inspector and to assign our animal prefabs as elements.
31
# 2.3.2 - Spawn an animal if S is pressed Write the If statement in the SpawnManager script for when the 'S' key is pressed.
if(Input.GetKeyDown(KeyCode.S));
32
# 2.3.2 - Spawn an animal if S is pressed Write the code to Instantiate the animalPrefabs array. Use 0 for the Array number, and set the spawn position to z = 20.
Instantiate(animalPrefabs[0], new Vector3(0,0,20), animalPrefabs[0].transform.rotation);
33
# 2.3.2 - Spawn an animal if S is pressed In the animalPrefabs game object in the Inspector, how do you choose how many elements to use?
Set the number at the right in the component to the number of elements you wish to utilize, and the element boxes will fill to that number.
34
# 2.3.2 - Spawn an animal if S is pressed When you've created a public array, you can assign prefabs to an Element in the Inspector. Each element is given a number. What is the term for these numbers? (e.g. Element 0, Element 1)
Index numbers
35
# 2.3.2 - Spawn an animal if S is pressed The code below will spawn Element 0 when the 'S' key is pressed. '0' is an Index Number in our array 'animalIndex' What do we need to change in the code to be able to spawn any object from the array? Instantiate(animalPrefabs[0], new Vector3(0,0,20), animalPrefabs[0].transform.rotation);
The index number needs to be changed to instead reference the name of the array. Instantiate(animalPrefabs[**animalindex**], new Vector3(0,0,20), animalPrefabs[**animalIndex**].transform.rotation);
36
# 2.3.3 - Spawn random animals from array We want to have the program randomly select the animals to spawn and not have us telling it what to spawn. What method do we need to use to accomplish this?
Random
37
# 2.3.3 - Spawn random animals from array If you have a variable you'll use throughout your code, where should you put it?
Put it outside of any methods (at the top)
38
# 2.3.3 - Spawn random animals from array What happens if we move a variable into a method?
It will only be seen within that method
39
# 2.3.3 - Spawn random animals from array What happens if we try to call a variable in the start method that we defined in the update method.
Unity will not recognize the variable and will not be able to call it.
40
# 2.3.3 - Spawn random animals from array What does the Random method do?
Used to generate random numbers
41
# 2.3.3 - Spawn random animals from array What is the Range method?
Will pull a number between the first and last numbers we select
42
# 2.3.3 - Spawn random animals from array If our elements we are using are labeled 0 -2 in range, then what numbers should we use for our Range? Why?
(0,3) The last number will not actually be counted. The last number is called an exclusive number meaning it won't be used.
43
# 2.3.3 - Spawn random animals from array In reference to an array, what does Length refer to?
Refers to the number of elements in the array or collection.
44
# 2.3.4 - Randomize the spawn location We want to spawn the animal within a random X range. In your Instantiate code, what part of the string would you modify, and what would you replace it with?
You would modify the Vector3 object position. Vector3 format is (x, y, z), so we modify the first position to modify the X values. We would replace the number here with Random.Range(x, x).
45
# 2.3.4 - Randomize the spawn location Write the code to Instantiate animal spawns with a random X-axis range, and set the Z axis to 20. Use the GameObject 'animalPrefabs' with index 'animalIndex'.
Instantiate(animalPrefabs[animalindex], new Vector3(Random.Range(-20,20, 0 ,20), animalPrefabs[animalIndex].transform.rotation);
46
# 2.3.4 - Randomize the spawn location Lets clean up our Instantiate code by writing a variable for our random spawn. Remove the Vector3 code from your Instantiate code and turn it into a variable called 'spawnPos'. Use Random.Range for your X with -20, 20, and make Z axis 20.
Vector3 spawnPos = new Vector3(Random.Range(-20, 20), 0, 20);
47
# 2.3.4 - Randomize the spawn location Remove the spawn position from your Instantiate code and turn it into a variable called spawnPos. Use SpawnRangeX for your X value, and spawnPosZ for your Z.
Vector3 spawnPos = new Vector3(Random.Range(-spawnRangeX, spawnRangeX), 0, spawnPosZ);
48
# 2.3.4 - Randomize the spawn location ** We've pulled the Vector3 position out of our instantiate code and created a varible: Vector3 spawnPos = new Vector3(Random.Range(-20, 20), 0, 20); Rewrite the Instantiate code to account for the variable. Use the GameObject 'animalPrefabs' with index 'animalIndex.
Instantiate(animalPrefabs[animalIndex], spawnPos, animalPrefabs[animalIndex].transform.rotation;
49
# 2.3.5 - Change the perspective of the camera **** Where do you go to change the perspective of your camera?
In the Camera object, In the inspector, go to the Perspective dropdown and select Perspective or Orthographic.
50
# 2.3.5 - Change the perspective of the camera What is Perspective view?
Objects appear smaller as they are farther from the camera, mimicking human vision and creating depth
51
# 2.3.5 - Change the perspective of the camera What is Orthographic view?
Objects stay the same size regardless of distance from the camera, removing depth and creating a flat, 2D-like view.
52
# 2.4.1 - Make a new method to Spawn animals What is void?
void is a return type that performs an action
53
# 2.4.1 - Make a new method to Spawn animals What is a method?
1. A block of code that performs a specific task 2. Can be called by its name.
54
# 2.4.1 - Make a new method to Spawn animals Why would we create our own, custom functions?
1. Makes it easier to read and manage 2. makes sure certain tasks you need for a specific purpose are only kept inside a specific method 3. can reuse as much as needed.
55
# 2.4.1 - Make a new method to Spawn animals We have the following code in the Update method: void Update() { if(Input.GetKeyDown(KeyCode.S)) int animalIndex = Random.Range(0, animalPrefabs.Length); Vector3 spawnPos = new Vector3(Random.Range(-spawnRangeX, spawnRangeX), 0, spawnPosZ); Instantiate(animalPrefabs[animalIndex], spawnPos, animalPrefabs[animalIndex].transform.rotation; } 1. Move the necessary code out of the Update method and create a new custom function for it. Name the new function SpawnRandomAnimal. 2. In the Update method, with the code for spawning removed, what needs to be added for when the S key is pressed?
void Update() { if (Input.GetKeyDown(KeyCode.S)) SpawnRandomAnimal(); void SpawnRandom Animal() { int animalIndex = Random.Range(0, animalPrefabs.Length); Vector3 spawnPos = new Vector3(Random.Range(-spawnRangeX, spawnRangeX), 0, spawnPosZ); Instantiate(animalPrefabs[animalIndex], spawnPos, animalPrefabs[animalIndex].transform.rotation; }
56
# 2.4.2 - Spawn the Animals at timed intervals What is InvokeRepeating?
Calls a specified method repeatedly at a set time interval.
57
# 2.4.2 - Spawn the Animals at timed intervals What is the code syntax used to write InvokeRepeating?
InvokeRepeating("MethodName", time, repeatRate);
58
# 2.4.2 - Spawn the Animals at timed intervals 1. Write an InvokeRepeating statement, and write the variables needed outside the method. You may name the method and variables whatever you wish. The start delay time is 1 second, the interval time is 2 seconds. 2. Write the syntax of the statement
private float startDelay = 1; private float spawnInterval = 2f; void Start() { InvokeRepeating("SpawnRandomAnimal", startDelay, spawnInterval); } 2. InvokeRepeating("method name", time, delayInterval);
59
# 2.4.2 - Spawn the Animals at timed intervals In our Animal Spawn project, what method should the InvokeRepeating statement be placed into in order to spawn random animals on a timer?
void Start()
60
# 2.4.2 - Spawn the Animals at timed intervals in the InvokeRepeating string, what does 'time' represent?
'time' = the delay before the first call
61
# 2.4.3 - Add collider and trigger components What do we need to add to an object if we want to add collision?
Collider component (Box Collider, Circle Collider, etc.)
62
# 2.4.3 - Add collider and trigger components What does the "Is Trigger" checkbox for and what does it do?
Is Trigger checkbox on a Collider determines whether the collider acts as a trigger. When checked: The collider won’t cause physical collisions but will detect overlapping objects and call trigger events like OnTriggerEnter(), OnTriggerStay(), and OnTriggerExit(). When unchecked: The collider will cause physical collisions and use collision events like OnCollisionEnter().
63
# 2.4.3 - Add collider and trigger components What is a RigidBody component?
A Rigidbody is a component that enables a GameObject to be affected by physics, such as gravity, forces, and collisions. It allows objects to move and rotate realistically within the physics engine.
64
# 2.4.3 - Add collider and trigger components In our feeding animals game, why might we specifically want to add a Rigidbody to our projectile food object, and not the animal objects?
Because we want the projectile to keep moving through the scene, we want Gravity to be turned off. Gravity is a toggle within the Rigidbody component.
65
# 2.4.4 - Destroy objects on collision What is OnTriggerEnter
OnTriggerEnter is a Unity event method called when another collider enters a trigger collider attached to the object (with Is Trigger enabled).
66
# 2.4.4 - Destroy objects on collision What is the syntax for OnTriggerEnter?
void OnTriggerEnter(Collider other)
67
# 2.4.4 - Destroy objects on collision What is Destroy?
Destroy is a Unity method used to remove a GameObject, component, or asset from the scene or memory.
68
# 2.4.4 - Destroy objects on collision What is the syntax for Destroy?
Destroy(gameObject);
69
# 2.4.4 - Destroy objects on collision Describe what is happening in this code: void OnTriggerEnter(Collider other) { Destroy(gameObject); Destroy(other.gameObject); }
// Destroys the object this script is attached to // Destroys the object that entered the trigger
70
# 2.4.4 - Destroy objects on collision Destroy(other.gameObject); - what is this referring to? What does Other stand for in this statement?
refers to the GameObject that owns the collider that triggered the event, telling it to destroy the other object it came in contact with. other is the parameter passed to the method: it's the Collider of the object that entered the trigger.
71
# 2.4.5 - Trigger a "Game Over" message Where can you go in Visual Studio to fix any indentation issues that might be in your code?
Edit > Advanced > Format document
72
# 2.4.5 - Trigger a "Game Over" message What is Debug?
Debug is a Unity class used for logging messages, warnings, and errors to the Console during development. It helps with testing, troubleshooting, and tracking code behavior. Common methods include: Debug.Log("message") – prints a message. Debug.LogWarning("warning") – prints a yellow warning. Debug.LogError("error") – prints a red error.
73
# 2.4.5 - Trigger a "Game Over" message What is Debug.Log
Prints a message to the Console
74
# 2.4.5 - Trigger a "Game Over" In DestroyOutOfBounds.cs, write the else-if condition that checks if the animals reach the bottom of the screen, add a game over message. Use lowerBound for your variable Hint: Objects are moving up or down along the z axis.
void Update() { if (transform.position.z > topBound) { Destroy(gameObject); } **else if(transform.position.z < lowerBound) { Debug.Log("Game Over"); }** }