Perception System Explained

Here is a bit explanation how the perception system code works.

Basic features of the code is as follows

  • If the player object is behind some other object, it is not seen. This is tested with Physics.Linecast from the center of perceiving object to the center of the player object.
  • If player object is really near (distance < closeRange), the player object is noticed 360 arc.
  • If the player object is fron (angle between the forward vector of perceiving game object and the player object is less than front angle) and distance is less than frontDistance, the player object is seen.


Simple Perception System (Unity, C#)

Here is a very simple perception C# code that I used my lecture today for Unity.

Function that can be added to the Enemy class to check if the enemy sees the player object or not (the code for other functionality can be found on the post /2011/10/29/intro-to-gameplay-programming-with-unity/). With this code, an enemy can perceive player object farther away if the player object is front and when rear on the side perseption range is more limited.There is also very simple line-of-sight checking functionality.

The features of the perception system can be controlled via class variables

public float frontRange = 10.0f;
public float frontAngle = 30.0f; // if angle between forward and los is less than this, the target is at front
public float closeRange = 2.0f;

Then the function actually handling the if enemy can see the player object or not.

public bool IsPlayerWithinPerceptionRange()  {
   RaycastHit hit;
   bool playerWithinPerceptionRange = false;
   if(Physics.Linecast(transform.position, GameAgents.GetPlayer().transform.position, out hit)) {
        if(hit.transform == GameAgents.GetPlayer().transform) {
            if(hit.distance <= closeRange) {
                 playerWithinPerceptionRange = true;
                 Debug.DrawLine(transform.position, hit.transform.position,;
            else if(hit.distance <= frontRange) {
                 if(Vector3.Angle(transform.forward,hit.transform.position - transform.position) <= frontAngle) {
                     playerWithinPerceptionRange = true;
                     Debug.DrawLine(transform.position, hit.transform.position,;

    return playerWithinPerceptionRange;

Intro to Unity GUI

This post contains an solutions  to my Unity programming exercises and classes I showed at the lectures. This post cover coding some basic things about using Unity GUI system.

StartScreenGUI (C#)

This class creates simple re-sizable and skinnable start screen for a game with main screen, credits screen, and confirm screen for quitting. The main screen is show in the Figure (with the textures and skin setup).

Read more

Lies and Seductions Source Code

Edit Nov 13, 2010: Unity project is now updated to 3.0. The source code is old 2.1 version without newer bug fixes included into the project and to the current version of the Lies and Seductions game. License details can be found in the Licenses folder. The source is mostly available under Apache 2.o license and assets under Creative CommonsAttribution-Noncommercial 1.0 Finland, but check for exceptions.

The current version of Lies and Seductions source code is now available and it is mainly released with Apache License, Version 2.0; refer to source files for details. The code is in a large extend uncommented and we are still working to fix bugs.

I’ll post an unity project file when I manage to create one. Now export, for some reason, dies with an error.

Edit: The source code download link is on the bottom of /games/lies-and-seductions.

Edit: Unity projectavailable (~240M), at least for some time.


The source includes:

  • NPC AI code including Texas Hold’em Poker AI.
  • Dialogue AI and XML based dialogue definition handling.
  • Navmesh based pathfinding and editor class for creating navmeshes (pathdinding is based on Pedro Teixeira code,