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)
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.
transform.Translate(Vector3.forward * time.Deltatime * speed)
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
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)
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
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.
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.
public float speed = 10.0f
2.2.1 - Make the projectile fly forwards
What is a Prefab?
An asset we can keep re-using anytime, anywhere and it will always have the same behavior
2.2.2 - Make the projectile into a prefab
What happens when you drag an object to your Prefab folder?
Unity will ask if you want to create new or create variant
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?
public GameObject projectilePrefab
Gameobject is a unity class, projectilePrefab is the name we are giving the variable that will be viewable in the Inspector.
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?
You want to drag the projectilePrefab from the folder to the component in the Inspector, on your PlayerObject.
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?
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( )
{
}
2.2.3 - Test for spacebar press
Write the code in the PlayerController script to launch projectile when the space bar is pressed down.
if (Input.GetKeydown(KeyCode.Space));
2.2.3 - Test for spacebar press
What does GetKey do?
GetKey makes Unity’s Input class check whether a specific key on the keyboard is being held down during the current frame.
2.2.3 - Test for spacebar press
What is the difference between GetKeyDown and GetKeyUp?
GetKeyDown checks for the frame the key is first pressed. GetKeyUp checks for the frame the key is released.
2.2.3 - Test for spacebar press
What does KeyCode do?
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.
2.2.3 - Test for spacebar press
What is happening in this code?
if (Input.GetKeydown(KeyCode.Space));
The code checks if the Spacebar key was pressed down during the current frame.
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.
{
Instantiate(projectilePrefab, transform.position, projectilePrefab.transform.rotation);
}
2.2.4 - Launch projectile on spacebar press
What is Instantiate?
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.
2.2.4 - Launch projetile on spacebar press
What is the code syntax for Instantiate?
Instantiate(Object original, Vector3 position, Quaternion rotation)
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.
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.
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.
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.
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?
Attach the same MoveForward script that the projectilePrefab uses to move to each of the animals. Preferably before moving it to the prefab folder.
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
if(transform.position.z > topBound)
{
Destroy(gameObject);
}
2.2.6 - Destroy projectiles offscreen
Write a new variable to define topBound with a value of 30.
private float topBound = 30.0f
2.2.7 - destroy animals offscreen
Write a new variable lowerBound so we can destroy animals past a certain Z value
HINT: negative value
private float lowerBound = -10.0f
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.
else if (transform.position.z < lowerBound)
{
Destroy(gameObject);
}