Week 11

Do Teacher Evaluations!

I helped Anthony with some wall running code. It's pretty clever.
wall running code
	using System.Collections;
	using System.Collections.Generic;
	using UnityEngine;
	using UnityStandardAssets.Characters.FirstPerson;
	public class wallrunning : MonoBehaviour {
	    private bool isWallR = false;
	    private bool isWallL = false;
	    private RaycastHit HitR;
	    private RaycastHit HitL;
	    private int JumpCount = 0;
		private RigidbodyFirstPersonController  cc;
	    private Rigidbody rb;
		public float runTime = .1f;
		void Start () {
			cc = GetComponent <RigidbodyFirstPersonController> ();
			rb = GetComponent <Rigidbody> ();
		}
		void Update () {
			if (cc.Grounded) {
		JumpCount = 0;
			}
		if (Input.GetKeyDown (KeyCode.Space) && !cc.Grounded && JumpCount <= 3)	{
			if (Physics.Raycast (transform.position, transform.right, out HitR, 1 )){
			if (HitR.transform.tag == "Wall") {
					 isWallR = true;
					 isWallL = false;
				 JumpCount +=1; 
				 rb.useGravity = false;
				StartCoroutine (afterRun ());
				Debug.Log ("wallrun R works");
				}
			}
			if (Physics.Raycast (transform.position, -transform.right, out HitL, 1 )){
			if (HitL.transform.tag == "Wall") {
					 isWallL = true;
					 isWallR = false;
				 JumpCount +=1; 
				 rb.useGravity = false;
				StartCoroutine (afterRun ());
				Debug.Log ("wallrun L works");
					} 
				}
			}
		}
		IEnumerator afterRun () {
			yield return new WaitForSeconds(runTime);
				isWallL = false;
				isWallR = false;
				rb.useGravity = true;
			}
		}
 
Here's a Inventory Tutorial for Alex.

Player prefs

Player prefs are variable that save to a file that can be accessed at the start of a new game or between levels. It's a way of keeping a player's preferences or a high score between sessions.
Let's make a "click the button" game that automatically saves how many times we have clicked the button, saves the number and reloads that it if we start playing again.
Start by creating empty game object and putting a new script on it called clickCount. In void start you want to check to see if there's a player pref file and if there is get the value and if there isn't create one. You use the keyword "KasKey" to do this. Don't forget to add using UnityEngine.UI; at the top. You use SetInt and GetInt to get and set the variables. (You can create player prefs that are floats, strings bools, you name it).
PlayerPref
	using System.Collections;
	using System.Collections.Generic;
	using UnityEngine;
	using UnityEngine.UI;
	
	public class clickCount : MonoBehaviour {
		public int currentScore; 
		void Start () {
			if (PlayerPrefs.HasKey("counter")){
				currentScore = PlayerPrefs.GetInt("counter"); 
			}
			else{
				PlayerPrefs.SetInt("counter", 0); 
				currentScore = 0; 
			}
		}
	}
Add a public function to the code that makes the variable go up by one. Set the playerPrefs to the currentScore at the same time.
public void click () {
	currentScore ++; 
	PlayerPrefs.SetInt("counter", currentScore);
}
Now we need a UI button to add to the score.
menu items for buttons

Make a button

Create a button Using UI buttons to swap scenes
change scene
	using System.Collections;
	using System.Collections.Generic;
	using UnityEngine;
	using UnityEngine.SceneManagement;
	
	public class changescene : MonoBehaviour {
		public void swapScene(){
			if(SceneManager.GetActiveScene().name == "scene1") {
				SceneManager.LoadScene("scene2");
			}
		}
	}

Don't Destroy

This code
DontDestroyOnLoad(gameObject);
will make whatever game object it's attached to not destroy itself when a new scene is loaded. This could be handier than player prefs. Most games will create a game manager object with code that holds variables that need to persist over every scene. Put this line in the ware or start function of the root gameobject (the parent) and all the kids will follow suit.
Another killer app for this would be on an audio track that plays under your entire game.
Do I feel a little guilty not telling Michael about this code earlier. Yeah, a bit. Sorry Michael.
Add a script with this don't destroy code to the canvas that holds the buttons and it will still be there in the next scene. Not working? Both scenes need the EventSystems Game object that gets added when you make a UI gameObject. You can add this to both scenes, make the eventSystem a child of the canvas, or make the event system set to don't destroy on it's own.

Singletons

The problem with don't destroy is, what happens when if you double back to a scene that already has the original object on it? Now there are two instances of the object, and both will never be destroyed. This is where Singletons came in handy. A Singleton is an instance of a class that's designed to only be called once. If it meets another instance of itself it kills itself so there can be only one. Yeah, it's pretty metal, deal with it.
Singleton
	using System.Collections;
	using System.Collections.Generic;
	using UnityEngine;
	
	public class keeper : MonoBehaviour {
	
		public static keeper keeperInstance; 
		private static bool created = false;
		void Awake () {
			if (keeperInstance == null){
		      keeperInstance = this; 
			  DontDestroyOnLoad(gameObject); 
			}
			else{
				Destroy(gameObject); 
			}
			created = true; 
		}
	}