Week 2


Problem: how to tell which way our block is facing? Our block needs some sweet fins! Create a tail fin for the cube and place it in the "back" on one side. Duplicate it and drag the copy to the other side. In the hierarchy, drag the fins onto the cube so that they are "children" of the cube. Now the fins will move as the cube moves. Parenting objects
In the image the taillights are children of the fins which are children of the ship. If you change the location or rotation of the ship all the children will change too. Children do not need to be touching their parent.

cheap n' easy 3rd person camera

If you want to make a simple 3rd person camera, align the camera so that it is directly behind the cube, and then make the camera a child of the cube.

Our First Camera Script

Let's say we want a camera that moves with the cube, but does not rotate with the player. You would need code that.

Problem: The code is on the camera but we need to know about the location of the cube, so we need some way to have the cube's transform data show up in the code.

Solution: create a public GameObject variable in the code. In the inspector drag the cube into the GameObject slot.

Note: you write GameObject with a capital 'G' when you're declaring a variable in your code, but you write gameObject with a lower 'g' when referring to the gameObject that the code is attached to.
GameObject obj = new GameObject(); //referencing the class and declaring an object
gameObject.GetComponent <Script>(); // referencing the gameObject that the script is attached to

Simple Camera: Attach to Main Camera
	using System.Collections;
	using System.Collections.Generic;
	using UnityEngine;
	public class movecamera : MonoBehaviour {
	   public GameObject player;
	   private Vector3 offset;
	   void Start ()
	   	offset = transform.position - player.transform.position;
	   void LateUpdate ()
	   	transform.position = player.transform.position + offset;
where to attach gameobject in inspector
Don't forget to attach the gameObject to the space in the inspector that you created

One new thing here is LateUpdate, which happens after all other code has happened. This ensures that the camera moves LAST. Not a big deal in our small game, but once a lot of things are happening it could save some heartache.

Let's make the game better.
Make short "bumper" blocks around the outside of the "rink" to keep the car in play. Attach a rigidBody but un-check Gravity and check the "Is Kinematic" button. If an object is Kinematic it will not be governed by the physics engine. So the car can bump into it without moving the block.
(The other way to do this would be to just not apply a rigidBody.)
Question, how would you make it so the walls are "bouncy"?

Collision Detection

Make a "Snitch" ball that will give us a point when we run into it - then disappear. There's a couple places we could put the code, on the ball or on the player. Let's put it on the player for now.

"Single Responsibility" Guideline

In Processing one tends to end up with long code. As you learn to structure code better you create custom functions not just to handle tasks that repeat, but to make the code easier to read. In Unity, it's good practice to have a script only do one thing if possible. This fits with the component paradigm and one object may end up with several scripts attached to it. Of course your mileage will vary but it's a good mindset to get into. In this case it's kind of borderline - truth is I would probably put collision code with the move code, but lets make a new script for fun.
Start with having it just write to the console when a collision happens.
Collision 1: attach to player object
	using System.Collections;
	using System.Collections.Generic;
	using UnityEngine;
	public class snitchCollision : MonoBehaviour
		void OnTriggerEnter(Collider col) 
			Debug.Log("joe is good"); 
Unlike Processing, C# does not require a Start and Update routine be applied to them.

OnCollisionEnter (Collision col) and OnTriggerEnter(Collider col)

There are two main functions for checking collision, OnCollisionEnter (Collision col), and OnTriggerEnter(Collider col). The major difference is that Trigger works before the physics happens. So of you want the prize to disappear and not effect the player this is the one to use. If you were making rocket league clone where the ball bounces away from the player then you'd use OnCollisionEnter.
notice that trigger uses 'Collider' in the parenthesis and collision uses 'Collision' - this screwed me up for hours one day.

The script will trigger when it hits something that has the "is trigger" checkbox on in the collider. In our game so far we only have one thing with the trigger turned on, but in theory we could have many and we'd need to identify that this is indeed a prize. We can get the name of the object it's hit, but what if we want a bunch of prize objects not just one? That's where tags come in handy.


Tags are a way of organizing and identifying objects. The tags pull-down menu is visible at the top of the inspector when you select a GameObject. First you need to create a new tag, then assign it to the object. Now we can check to see if the gameObject that the player hit is a prize or not.
if (col.gameObject.tag == "prize"){
Debug.Log ("joe is good");
if(col.CompareTag("prize")) also works but only with the trigger whereas gameObject.tag works with both trigger and collider
Add in some code to keep score and the code to destroy the snitch.
keep score & destroy object
	using System.Collections;
	using System.Collections.Generic;
	using UnityEngine;
	public class snitchCollision : MonoBehaviour
		public int score; 
		void onStart(){
		score = 0; 
   		void OnTriggerEnter(Collider col) 
   			if (col.CompareTag("prize")){
   			score ++; 


Well here the Quidditch reference breaks down, but let's say you want to have several prize snitches. We could duplicate them in the hierarchy, and that would work, but if I decided to change the color you'd have to change the color of all of them, and that could be annoying. A better method would be to create what's called a PreFab. Notice that if you select the snitch there are new "preFab" buttons in the inspector. Make a change to a snitch and press the apply button and it will make the changes to every instance of the preFab.


The material changes what an object looks like. It does not affect the physics - for that use Physics Material.
Go to your project and right click select create - material. You'll probably want a materials folder in your project also.
Albedo is the color - change the color and name your material drag it onto the object. Done!
wall with brick texture

repeating image textures

Find and image of something you want to make as a repeating texture. A brick wall for example and open it in photoshop

Apply the texture to the material

You can put the same material on lots of objects, but remember, changing the material will change it everywhere.
Play with the Render Mode and also the Shader to get different looks. Give different object in our game different materials.

Problem. The material looks good on one side of the ojbect but not the other. Solution: 1. make a separate plane for side that looks bad and cover it up. 2. don't use 3d primatives, but instead use a 3D program to create your models. (comming soon!)
textures in the docs

Problems to work on:

Warning: these are not necessarily easy problem with one solution.