Due 25 Oct 2016

  1. Build three prototypes in Unity using C# scripts
  2. You can use prototypes from the previous exercises, or new ideas
  3. If you use new ideas, make sure to go through the game brainstorming/game design document/punch list plan as before

Twitch Stream

To give you a concrete sense of the concepts we're talking about, I will be streaming my own prototyping session this Saturday (the 24th) at 3pm on my twitch channel. If you can make it, stop by and watch me work. If you can't, that's cool too. I will be recording the session and it will be made available to you to rewatch and study at your leisure.

C# Scripts

We talked a lot about the underlying semantics of C# last week. It's important to have a sense of what is happening, but here's some more practical information we didn't get to:

Cheat Sheet

We will look at the Unity API more closely next week, but as a head start here's a cheat sheet I made last year that might be helpful. The syntax is UnityScript, but the concepts are the same. I should have a C# cheat sheet for you by next week.

Examples

Taking Input

A common task in game development is reacting to user input. There is no input event or message in Unity, but there is Update and the Input class. We can check every frame if a key is pressed and react to that. Here's a script that moves up, down, left, right depending on the keys pressed. Look at the documentation on GetKey and Translate for more information.

using UnityEngine;
using System.Collections;

public class Mover : MonoBehaviour {
  public void Update () {
    if (Input.GetKey ("up")) {
      // up key is pressed, move up
      transform.Translate (0, 1, 0);
    } else if (Input.GetKey ("down")) {
      // down key is pressed, move down
      transform.Translate (0, -1, 0);
    } else if (Input.GetKey ("left")) {
      // down key is pressed, move down
      transform.Translate (-1, 0, 0);
    } else if (Input.GetKey ("left")) {
      // down key is pressed, move down
      transform.Translate (1, 0, 0);
    }
  }
}

Removing GameObjects

Another common response to an event is to remove a GameObject. If an object is "picked up" or "destroyed" in the game you might want to do this. Here's a script that removes everything it touches from the game. It needs to be attached to a GameObject that also has a 2D collider component (e.g. a BoxCollider2D or a CircleCollider2D) with Is Trigger set to true and a Rigidbody2D. Look at OnTriggerEnter2D, Collider2D, and Destroy for more information.

using UnityEngine;
using System.Collections;

public class RemovesEverything : MonoBehaviour {
  void OnTriggerEnter2D(Collider2D other) {
    Destroy (other.gameObject);
  }
}

Playing A Sound

Unity scripting is all about coordinating components. Here's a script that plays a sound when an object is moving too fast because of physics.

  1. It asks the Rigidbody component (the built in component responsible for physics) how fast its going, or more specifically what the magnitude of its velocity is
  2. If it is above some amount (controlled by the public tooFast variable), it asks the AudioSource component to Play whatever clip is has associated with it. You need to set up the Rigidbody and AudioSource components in the Editor.
using UnityEngine;
using System.Collections;

public class ScreamWhenTooFast : MonoBehaviour {
  public float tooFast;
  void Update() {
    float howFast = GetComponent<Rigidbody> ().velocity.magnitude;
    if (howFast > tooFast) {
      GetComponent<AudioSource> ().Play ();
    }
  }
}