Week 1

Unity Layout

There are a bunch of windows in unity. Getting to know them by name will make things easier.
unity window
If you accidentally close a window all of these (and more) are also under ... wait for it ... "window".

Navigating the scene

Make a plane and a cube so we have something to look at (go to: GameObject - 3D Object and both Plane and cube are there.) It will try to put these objects near the center of what you are currently looking at in the scene view. The default scale will be 1. One what you ask? It's meters. Suck it America - you lose! (sorry).

using a scroll wheel 3 button mouse

Super handy f'ing trick for navigating

Select object in the hierarchy. Move the mouse over the scene. Press the letter 'f' and the view will shift to showing that object. This can be a life saver for finding stuff.

Super handy Trick for orienting the camera exactly how you want

In the scene orient your view so you are seeing what you want the camera to see. Select the camera in the hierarchy. Go GameObject - Align With View (it's near the bottom). You're welcome. When you click on an object you will be able to do different things with it depending on what tool you have selected.


tools pallet
There are five tools in the top left for manipulating objects, but you'll mostly only use the middle three. The 'qwert' keys will also swap between tools.

gameObject and Transform


In unity speak EVERYTHING that can go into a scene is a GameObject of some kind. Lights, cameras, primitives, 3D models are all GameObject. You can even have an empty GameObject, but it's still a GameObject. Different GameObject will have different "components" attached to them in the inspector. Want to put a script on your block to make it move? That script will be a component. Want the block to make a sound - that's another component.
inspector detail


Every GameObject has a default component called a Transform and it's the one component you can't get rid of. The transform shows the location, rotation and scale of the object in the scene. All the stuff you can change with the tools you can also change here. Try it!
When we get to coding there will be times when we refer to the transform. This means the "Transform" component on the game object, not that an object is transforming. Yeah, it's a little annoying at first. BTW, did you know that Bill Gates apologized for control-alt-delete? Late, but better than nothing.


Making physics happen on stuff

When you put an 3D primitive object in a scene like a cube or sphere it will have five components already. Try putting the cube above the plane and pressing play. Congratulation, the cube is suspended in space. perfection has been achieved, class dismissed.
rigidbody dialog box
For physics to work on an object the object has to have something called a rigidBody. 90% of the objects you put in unity will need one, especially if it's moving. Play it again and the cube will suddenly have gravity. So cool!
Gravity and Mass are self explanatory, Drag is how much it slows it down, and angular drag is how much it slows down when turning.
isKinematic means that the Forces and collisions will not affect the rigidbody anymore. Why bother with a rigidBody if you don't want forces of collisions to work on it? Well you can turn this on and off in the code - it's actually really handy.

move with code

To move the block we need to attach a script to the block. There are a few ways to do this. * "But" you're saying, "I already know some javaScript, can't I use that instead?" No, because:1. C# is becoming the standard, 2. it's better 3. the "javascript" in Unity is pretty far from the javascript you already know so there's no real advantage. 4. trust me.
Warning nerd talk: Unity scripting is entirely object based. Everything is an instance of a class. (If this freaks you out then ignore it because you don't need to fully understand this to write code.)
hello world
		using System.Collections;
		using System.Collections.Generic;
		using UnityEngine;
		public class move : MonoBehaviour {
		// Use this for initialization
		void Start () {
	// Update is called once per frame
	void Update () {
Let's look at each line
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
These are like libraries in Processing. They extend the code. There will be some code we write that needs other things here but for the most part we can leave it alone.
public class move : MonoBehaviour {
Notice if you will the correct spelling of behaviour. Everything is going to happen inside of this function (because everything is an instance of a class. shhh). There is no direct analog to this routine in Processing, it's like an extra function that contains both setup and draw. The word 'move' is in this line because that's what we named the file.
NOTE: if we change the name of the file in Unity, we need to also change this line in the code. The error code will say "The referenced script on this Behaviour is missing!" (not super specific). You'll also need to update the reference in the inspector - upshot: name your code well so you don't have to change its name later cause it's a pain.
Because we all have some Processing I'm guessing that we all know what start and update do. (Also it tells you in the comments.)
make it go 1
   using System.Collections;
   using System.Collections.Generic;
   using UnityEngine;
   public class move : MonoBehaviour {
   private float locZ = 0f; 
   private float speedZ = 0.1f; 
   void Start () {
	void Update () {
	locZ += speedZ; 
	transform.position = new Vector3(0, .5f, locZ);
We've created two variables, locZ and speedZ that will be "global" to this script, meaning they will be available to every function in the script. We don't use the word "global" in Unity however, because the variables are not going to be available to ALL functions in ALL scripts so the term could be misleading. We'll get to what "private" means in a moment.
NOTE: control-i auto formats in MonoDevelop
Both variables are floats, and include an "f" after the number. Using the letter f to designate a float is common in a lot of languages, although will be new if you're coming from processing. The error message you will get if you forget to add the 'f' is actually pretty verbose, "Literal of type double cannot be implicitly converted to type `float'. Add suffix `f' to create a literal of this type."
transform.position = new Vector3(0, .5f, locZ);
The position is stored in a variable called a "Vector3" that has an x, y and z variable - similar to Processing's PVector.
You can't just set the transform.position.z, you need to make a new Vector3 and make the position that new vector.


public variable in inspector
Changing the variable speedX changes how fast it goes. But if you want to fine tune it going back to the code to change it each time can get a little tedious. If you change the variable from private to public the variable will show up in the inspector.
public float speedX;
Now you can change the speed without having to re-save the code each time. Also, you could apply the same code to a different block but give it a different speed. That's the primary difference between public and private - public variable show up in the inspector. NOTE: the inspector will overwrite the value you give the variable when you create it in code, however, if you give the variable a value in the start or update functions that will override the value in the inspector.
There are three advantages to having the variable in the inspector.
  1. You can easily change the variable during runtime
  2. You use the inspector to see live updates of the variable for debugging
  3. You can assign the same script to a different object but give the new object a different speed.
The big disadvantage is when you're staring at your code and you can't figure out why the FRACK the values are not what you set them to be because you forgot that you set the values in the inspector. Fortunately, not being an idiot, I've never had this problem, but my friends have told me about it.


The method we used to move the block above works, but in involves knowing where something is in relation to world space. Mostly we want to move something relative to where it currently is not relative to 0, 0, 0. One way you could move the block is by "translating the transform". Translate moves relative to its current position. This is a blessing.
transform.Translate(Vector3.forward * speedZ);
This lets the block move forward along the z (blue) axis. You can often get the same results using addForce and AddRelativeForce. For example, rigidbody.AddForce(transform.forward * 10); is the same as rigidbody.AddRelativeForce (Vector3.forward * 10); Also, not on this list is AddForceAtPosition - had to end the list somewhere. The transform is the only component that you don't need to use GetComponent< >() on, however, in Unity 4 and earlier you used to be able to just say rigidBody.addForce so you might see some old code out there that doesn't work. This is probably why.

FORCE: Another (usually better) way of moving

Moving the transform is fine, but it's not a good idea if you want the Physics engine to work properly on the object. Fortunately there are other ways to think about moving things in Unity where the physics engine can take full effect. To do this you add a force to the rigidBody (not the transform) of the object. This is like pushing the object in a certain direction, not setting it in an absolute spot.
Rigidbody rb = GetComponent<Rigidbody>();
rb.AddForce(transform.forward * speedX);
The graph below use a lot of different examples - if it's intimidating then ignore it for now. For brevity I used rb for the rigidbody, like in the example above.

Moving Chart

way to move details when to use it Example code
transform.position change location relative to world space if the object has no rigidbody transform.position = new Vector3(0, 0.5f, 200);
transform.Translate change location relative to the object if the object has no rigidbody transform.Translate(Vector3.forward * thrust);
rb.addForce push object using force in world coordinates If the object needs physics applied to it. Use forces in FixedUpdate, and try not to use it every frame. float speed = 1.5f
float thrust = Input.GetAxis("Vertical");
rb.AddForce(transform.forward * speed * thrust);
rb.AddRelativeForce push object using force in local rotation If the object needs physics applied to it. Use forces in FixedUpdate, and try not to use it every frame. rb.AddRelativeForce(Vector3.forward * speed * thrust);
rb.AddTorque spin using force use if object has a rigidbody float torque = 1.5f
float turn = Input.GetAxis("Horizontal");
rb.AddTorque(transform.up * torque * turn);
rb.position set to a location using rigidbody use if object has a rigidbody rb.position = Vector3.zero;
rb.MovePosition move to a location and consider collisions use if object has a rigidbody rb.MovePosition(transform.position + transform.forward * thrust);
rb.velocity add instant velocity to rigidbody use ForceMode.impulse instead
constant force component Component for simply moving an object Use code instead, ya wuss

types of force

Along with applying a force you can add ForceMode to change how it behaves as well. The default is ForceMode.Force.
rb.AddForce(transform.forward * speedX, ForceMode.Impulse);


Sometimes you will also see movement also in include Time.deltaTime, written like this:
rb.AddForce(transform.forward * Time.deltaTime * speedX, ForceMode.Force);
Time.deltaTime is the amount of elapsed time between frames, if the project drops frames for any reason, this will make sure the physics still work and it looks smooth. Think of it as doing the same thing a smoothing capacitor does in a DC circuit if a the power supply has a fluctuating signal. I don't care if you don't know what that means, it's a perfect analogy and I'm sticking with it.

public variable in inspector

Physics Material

The physics material has nothing to do with how it looks, just how it behaves. With the cube selected, go to the Inspector and find the Box Collider component. click on the tiny circle next to the box that says (none) physic Material (see image) . This will bring up a dialog box. Select "ice". When you play now the cube should slide instead of tumble.

Keyboard Input

Make it Go 2: Driving
	using System.Collections;
	using System.Collections.Generic;
	using UnityEngine;
	public class move : MonoBehaviour { 
	   private float locX = 0.0f; 
	   public float speed; 
	   private Rigidbody rb; 
	 void Start () {
	   		rb = GetComponent< Rigidbody>();
	void FixedUpdate ()
		float moveHorizontal = Input.GetAxis ("Horizontal");
		float moveVertical = Input.GetAxis ("Vertical");
		rb.AddForce(transform.forward * moveVertical * speed); 
		transform.Rotate(0.0f, moveHorizontal * speed, 0.0f);

The values moveVertical and moveHorizontal will return to 0 if nothing is pressed. This means that speed needs a value to work. Make sure you add a speed value in the inspector
FYI for most keyboard input you would use Input.GetKey like this
  void Update() {
   	if (Input.GetKeyDown("space")){
   	    Debug.Log("joe's great!")
but in this case using GetAxis is useful. It will do some work for us returning a value between -1 and 1. GetAxis gets values from the 'wasd' keys, and also a joystick, making it easy to switch between between the two. Some other changes, Note: Getting the forward vector3 works fine, but if our block was still tumbling instead of sliding then "forward" could be straight down. Here's another method to apply force in the correct direction. Instead of adding force, it adds force relative to the current position.
Vector3 movement = new Vector3 (moveVertical, 0.0f, 0);
rb.AddRelativeForce (movement * speed);