Unity Basics

Unity Logo

Are we recording?

Meow Cam

Review

What is a GameObject?

  • A collection of components
  • Has a name, tag and layer
  • Can be parented

Review

Name any 3 elements of the Unity Editor

  • Scene View
  • Game View
  • Project Window

GameObject parenting

  • Global Space - relative to the world center
  • Local Space - relative to the parent
  • Object Space - relative to the object

Prefabs

Make complex GameObject hierarchies reusable

  • Regular Prefabs
  • Prefab Mode
  • Prefab Variants
  • Nested Prefabs
  • Referenced as GameObjects

Coding Convention

  • camelCase for variables
  • PascalCase for methods and classes
  • Open braces on a new line

Further details here

The Event Loop

A set of functions executed over a scripts lifetime in a predetermined order by the engine

Further details for its Unity implementation here

MonoBehaviour

Base class for all Unity scripts. Determines what parts of the event loop the script will be in.

Most important messages*/properties are:

  • Start() - called on object instantiation
  • Update() - called once per frame
  • gameObject - the GameObject this component is attached to
  • transform - the Transform attached to this GameObject

Exposing member variables to the Editor

Allows for much faster and easier parameter tweaking

Can be done by:

  • Making the member variable public
  • Adding the SerializeField attribute

Important!

Values in the editor take precedence over values in the script

Modifying Components Through Script

Let's modify the transform component

  • position
    • Delta time
  • rotation
  • scale

Finding Components through script

						
	//Dragging and dropping from the editor
	GetComponent<ComponentType>();
	GetComponentInChildren<ComponentType>();
	GetComponentInParent<ComponentType>();
	FindObjectOfType<SomeType>();
						
					

Finding GameObjects through script

						
	//Again dragging and dropping from the editor
	GameObject.Find("SomeName");
	GameObject.FindObjectWithTag("SomeTag");
	transform.parent;
	transform.GetChild(int clildIndex);
						
					

GameObject Lifecycle

						
GameObject myGameObject = Instantiate(Object original, 
                                      Vector3 position, 
                                      Quaternion rotation);
//...
var component1 = myGameObject.GetComponent<ComponentType>();
//...
var component2 = myGameObject.AddComponent<ComponentType>();
//...
Destroy(myGameObject);
						
					

Keyboard Input

						
	// true if the key is pressed
	Input.GetKey(KeyCode.SomeKey); 

	// true the first frame the key is pressed
	Input.GetKeyDown(KeyCode.SomeKey); 

	// true the first frame the key is released
	Input.GetKeyUp(KeyCode.SomeKey); 
						
					

Axis Input

Configurable in Edit/Project Settings/Input Manager

						
	// "ButtonName" = "Fire1" for left mouse button
	Input.GetButtonXXX("ButtonName");

	// Smoothed input
	Input.GetAxis("AxisName");

	// Raw input
	Input.GetAxisRaw("AxisName");

	// "AxisName" = "Horizontal" for A/D
	// "AxisName" = "Vertical"   for W/S
						
					

Lets make a moving cube

Configurable in Edit/Project Settings/Input Manager

						
	Vector3 moveDirection = 
		new Vector3(Input.GetAxis("Horizontal"), 
		            0, 
		            Input.GetAxis("Vertical")) 
		            .normalized 
		* Time.deltaTime 
		* moveSpeed;
	Vector3 pointToLookAt = transform.position 
	                      + moveDirection * 100;

	transform.position += moveDirection;
	transform.LookAt(pointToLookAt);
						
					

Building the Game

  • Go to File/Build Settings
    • Ctrl/Cmd + Shift + B
  • Add your scenes
  • Select your target platform
  • Fiddle around with Player Settings
  • Click Build

Questions?

Question Cat