Week 10

scene with moody lights
Goal - We'll make a dark moody room with dappled light flowing though a window. There's a Security camera in the corner with a blinking red light on it. If you move into the lens of the camera it turns on a big red light. Text appears on the screen saying you've been detected. Once you go behind a wall the light turns off.

Lighting (more complicated)

Notes on lighting greyscale pattern cookie monster waving

live tv monitor

Use culling mask to make TV glow

light up the tv in a dark room (There may be better ways, but this works) click on your tv quad object create a layer called something super creative like "tvLayer" Assign the tvLayer to the TV. create a spot light and point it at the tv. set the culling Mask on the light to the tvLayer only. Magic! you may want to reduce the glare by changing the "smoothness" of the tv's render texture. you can tint the tv by enabling emission of the tv's render texture and adjusting the color.

Camera "sees" player

There are two steps to this process: determine if the player is in the camera view and determine if there is something between the player and the camera (obstruction the view). The script uses Linecast instead of Raycast. Raycast goes out from a location at a certain angle. Linecast draws a line between two points and sees if anything is between them. Similar ideas but in this case Linecast makes the most sense.
goes on 2nd camera
using UnityEngine;
using System.Collections;
public class securityCam : MonoBehaviour
{
	public static bool seePlayer; // public static so it's easy to get from other scripts
	public int EvilRobotNum;
	public GameObject player; // don't forget to attach the player gameObject

	void Update()
	{
		// this uses colliders so put a small collider on the 1st person controller.
		if (GeometryUtility.TestPlanesAABB(GeometryUtility.CalculateFrustumPlanes(GetComponent <Camera >()), player.GetComponent<Collider >().bounds)) 
		{
			RaycastHit hit; 
			if (Physics.Linecast(transform.position, player.transform.position, out hit)) // returns true if there is a collider between two points. 
			{
				if(hit.collider.tag == "playerTag"){
				    seePlayer = true; 
				}
				else{
			         seePlayer = false;
				}
			}
		}
		else // if we're not in view of the camera
		{
			seePlayer = false; 
		}	
	}
}
There's a cool debugging trick where you can see your lines and raycasts while the game is running if you switch over to scene view.
Debug.DrawLine(transform.position, player.transform.position, Color.green);
Put a point-light in the room and put this code on the point-light. Set the intensity to 0 in the inspector.
goes on 2nd camera
	void Update ()
	{
		if (securityCam.seePlayer) {
			GetComponent<Light>().intensity = 2; 
		} else {
			GetComponent<Light>().intensity = 0; 
		}
	}

InvokeRepeating to make blinking timer

Put a blinking light on the security camera. Use a small red point-light. Add a halo if you want. There are a few ways to make it blink, but here's a Unity specific way that's pretty handy using "InvokeRepeating". It takes 3 parameters, the function it will run, when to start and how often to repeat. Notice that it goes in start not in update.
goes on a light
	public bool lightOn; 

	void Start () {
		InvokeRepeating("switchlight", 2.0f, 2.0f);
	}

	void switchlight(){
		lightOn = !lightOn; 
		if(lightOn){
			GetComponent<Light>().intensity = 5; 
		}
		else{
			GetComponent<Light>().intensity = 0; 
		}
	}
Adding a the UI element is on week 3.

multiple cameras

IN c# you can use "enabled" to turn cameras on and off. Keep an int of the current camera and swap between them, turning the current one on and all the others off. You could do this with a bunch of if statements but you could use a switch statements instead for easier to read code.
goes on a light
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement; // you need this! 

public class cameraSap : MonoBehaviour
{
    public Camera Cam1, Cam2, Cam3;
    private int camNum = 1;
    // Use this for initialization
    void Start()
    {
        Cam1.enabled = true;
        Cam2.enabled = false;
        Cam3.enabled = false;
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            camNum++;
            switchCam();
        }
     }
    void switchCam()
    {
        if (camNum == 4)
        {
            camNum = 1;
        }

        switch (camNum)
        {
            case 1:
                Cam1.enabled = true;
                Cam2.enabled = false;
                Cam3.enabled = false;
                Cam4.enabled = false;
                break;
            case 2:
                Cam1.enabled = false;
                Cam2.enabled = true;
                Cam3.enabled = false;
                Cam4.enabled = false;
                break;
            case 3:
                Cam1.enabled = false;
                Cam2.enabled = false;
                Cam3.enabled = true;
                Cam4.enabled = false;
                break;
        }
    }
}


UI graphics and keeping score menus culling profile and debugging underwater physics RAycasting pick up carry put down object.