Tutorial: 1st-person sneak in Unity 5, part 1

This tutorial goes through how to build a 1st person sneaking game with simple enemies/guards that patrols predefined routes and if they spot the PC they start to follow and attack if they get near enough. A short video clip demonstrates the guard behaviour this tutorial builds.

This tutorial assumes that you are familiar with Unity and can create game objects, prefabs, and scripts.  The code snippets below are in C#.

Read more

Colliders and Different Methods of Moving Objects in Unity

One problem with students starting to work with Unity is to understand when colliders work and when they do not work. This very simple demonstration shows how Physics::AddForce(), CharacterController:: SimpleMove() and Translate.Transform() behaves with colliders. Moving a GameObject using Physics and CharacterController based methods interacts with collisions whereas Transform::Translate() does not as seen in the video. Moreover, Translate does not work with MonoBehavior::OnTriggerEnter().


Physics::AddForce():

public class PhysicMove : MonoBehaviour {

	void Start () {
		rigidbody.AddForce (new Vector3 (100, 0, 0));
	}
}

CharacterController::SimpleMove()

public class Move : MonoBehaviour {

	public float speed;

	private CharacterController controller;

	void Start () {
		controller = GetComponent ();
	}
	
	void Update () {
		controller.SimpleMove (new Vector3 (1, 0, 0) * Time.deltaTime * speed);
	}
}

Transform::Translate()

public class MoveTranslate : MonoBehaviour {

	public float speed;

	void Start () {
		enabled = false;
	}

	void Update () {
		transform.Translate (new Vector3 (1, 0, 0) * Time.deltaTime * speed);
	}
}

An Experiment in Teaching Level Design

I wanted to introduce some unconventional design approaching while teaching level design. I have had an idea to do a game with using a surrealist  approach. For that I designed a game stub and coded the base functionality on the top of Unity. The student are tasked to do level design using that design, code, created prefabs.

My slides for the course intro (http://www.slideshare.net/lankoski/level-designintro)

Testing Public Variables in Unity

Edit 2012/12/08: A bug fix (End() did not show correct error count); change reporting: now EndCase() and End() use Debug.LogError() fuction in reporting if there were errors (otherwise Debug.Log() is used).

It is easy to forget to set a public variable of a script in Inspector and it might take some time to figure out where the problem is. The following C# program goes and checks that all public variables in a scene are set.

TestMonoBehaviorPublicVariables.cs

using System.Collections;
using System;
using System.Reflection;
using System.ComponentModel.Design;

public class TestMonoBehaviorPublicVariables : MonoBehaviour { 
    private int errCount;
    private int errCountInScript;

    void Assert(bool status, string message) {
         if(status) { 
             Debug.LogError(" Test case failed: " + message);
             errCount++;
             errCountInScript++;
         }
    }
    void EndCase(string gameObjectName, string scriptName) {
         if(errCountInScript > 0) {
		Debug.LogError(gameObjectName + " - " + scriptName + ": errors=" + errCountInScript.ToString());	
	}
	else {
		Debug.Log(gameObjectName + " - " + scriptName + ": errors=" + errCountInScript.ToString());
         }
         errCountInScript = 0;
    }

    void End(){
         if(errCount > 0) {
            Debug.LogError("Testing MonoBehavior public variable: errors=" + errCount.ToString());
         }
         else {
            Debug.Log("Testing MonoBehavior public variables: errors=" + errCount.ToString());
        }
    }

    void Start () {
         errCountInScript = 0;
         errCount = 0;
         UnityEngine.Object[] all = Resources.FindObjectsOfTypeAll(typeof(GameObject));
         foreach(GameObject go in all) {
             if(go.tag != "DontLook") { 
                 Component[] allComponents = go.GetComponentsInChildren<Component>();
                 foreach(Component c in allComponents) {
                       try{
                           RunCase(c);
                       }
                       catch(Exception e) {
                           Debug.LogException(e, go);
                       }
                 }
             } 
         }
         End();
    }

    void RunCase(Component c) {
         FieldInfo[] myField = c.GetType().GetFields();
         for(int i=0; i<myField.Length;i++) {
             string msg = String.Format("{0} - {1} ({2}): field {3} is not set", c.name, myField[i].Name, myField[i].DeclaringType, myField[i].Name);
             Assert((myField[i].GetValue(c)).ToString()=="null", msg);
         }
         EndCase(c.name, c.GetType().ToString());
    }
}

Create prefab for this script so that you can easily drop it to a scene and test all the objects. The problems are reported as errors in Console (error counts for each object and scrip are also reported as well as total number of found errors).

Drop the prefab to a scene, hit “Play” (and stop immediately), and inspect the Console.

Introduction to Unity

 

The introduction tutorial uses accompanied Unity packages:

Update 2013/01/07. file hosting changes and URLs changes to point to the new location.

Update 2012/10/27: The slides and packages now include GameManager and MainScreenGUI prefabs. GameAgents script handles Player adding player object to the scenes so that testing is easier. Tutorial slides reflect these changes.

Unity Examples

EDIT 20130525: updated links to Escape package.

I have created a small Unity package for teaching unity basics. You can download it at

To use this create new unity project (File->New Project…). Select  Toon Shading (and Image Effects if you have Pro) package(s) before creating the project.

Unzip the escape package and add it to the project (Assets->Import Package->Custom Package…).

I will add a tutorial using the prefabs and materials in the package later. Meanwhile, some introduction to the code is available in previous post (these mainly explain the principles, the current package does not match exactly to the codes):

Update1: the package is made with Unity 3.5.6.

Update2: added package that works with free version of Unity.