Week 4 Tuesday
Version control concepts and vocabulary
Thinking like a programmer
Design journal
References
Using version control in your project:
We will use a system called Perforce to turn in homework starting soon.
We will just be using it as a way to trade files for a while. Later in the semester we will go through how to set it up with Unity projects so that you can work with a partner.
Perforce is a centralized system of version control – there's a central server (perforce.sca.usc.edu) where files live.
To keep things tidy, each class session at SCA gets its own area on the server, called a depot. (Other version control systems call this a repository.)
Then there are clients (individual laptops, lab machines, etc.) that can copy files from the server, make changes and send them back.
Unlike Google Drive, in Perforce you can't make changes directly to what's on the server. You can't make a new document or upload a file to a particular place.
The only way to communicate with the Perforce server is to sync – that is, you make changes to what's on your computer and then push those changes to the server.
When you push a bunch of changes up to the server, you send them as a single bundle called a changeset.
A changeset can be one or several files that you've updated, moved, added, or (gasp) deleted.
Perforce saves a copy of every changeset, so that you can get back to earlier versions of your files if there is a problem.
We will use a program called P4V to interact with our Perforce server. If you want to use it on your own computer, then you'll download it as part of the homework.
P4V lets you see what's on the server and what's in your local copy of those folders, that is, your workspace. You will use it to get files, like demo projects for class, and to push files, like your homework.
Here are three ways to decide what the other object is, in a collision:
Note that there are many others.
if (collision.gameObject.name == "Goomba") { Die(); }
Pros:
Cons:
Pros:
Cons:
if (collision.gameObject.tag == "Goomba") { Die(); }
Another way to know whether some GO is the kind of GO you care about: ask for its script components.
collision.gameObject.GetComponent<Goomba>()
If the other GO has a Goomba script attached, you can assume it's a Goomba.
collision.gameObject.GetComponent<Goomba>()
If the other GO has a Goomba component, this will get you a reference to it.
If the other GO does not have a Goomba component, this will return the special value null, which is nothingness.
So to determine whether the other GO has the script, you ask if the result is not null.
Not-nothing is something. So it must be a Goomba.
Goomba potentialGoomba =
collision.gameObject.GetComponent<Goomba>();
if (potentialGoomba != null) { Die(); }
Pros:
Cons:
if (potentialGoomba != null) { Die(); }
are files
Prefabs are necessary in order to spawn (create) new things during your game – enemies, bullets, etc.
Why? Because Unity needs to have a template to work from when you tell it to make something new.
Once you have a prefab to work with, you can stamp out as many copies as you like.
There are two steps.
First, in the script that is going to spawn new objects, give yourself a [SerializeField] variable to hold a reference to the prefab.
[SerializeField] GameObject blockPrefab;
This will usually be of type GameObject.
But later on you will sometimes find it more useful to use another type.
Then you can use Unity's Instantiate function to crank out the new objects.
Instantiate (blockPrefab);
This is the simplest version. It will make a new copy of the prefab. The new object's position will be whatever the prefab has as its position.
Instantiate ( blockPrefab, position, rotation );
Often you want to set the position of the new object at the moment you make it.
But this function requires you to set a rotation as well.
What to make (prefab)
Where (V3)
How to rotate it (Quaternion)
About 99% of the time you will use one of these two values for the rotation:
Quaternion.identity
transform.rotation
no rotation
use this GO's rotation
You will use these two versions of the Instantiate function about 90% of the time.
There are others that let you set the parent of the new object, or all of the above. See the documentation.
But you can do much more with that newly spawned object... if you save a reference.
Instantiate ( splatPrefab, position, rotation );
Other times you will want to remember the thing you instantiated.
Sometimes it is just for a moment, so you can set its properties. Other times you want to keep the reference for longer. Either way, this is how you save a reference to the new object:
Instantiate ( thingPrefab, position, rotation );
GameObject newThing =
Once you have the reference to the new instance, you can do anything you can do with a GameObject or its components.
newThing.transform.parent = gameObject.transform; newThing.GetComponent<Renderer>().enabled = false;
Remember, your scripts are components too. Here is how to get a script component from a newly instantiated object and call a method on it.
Instantiate (...);
HomingMissile newMissile =
newMissile.SetTarget(player);
assumes the HomingMissile script has a public method called SetTarget
GameObject newMissileGO =
newMissileGO.GetComponent<HomingMissile>();