Skip to main content

Quickstart Tutorial

5-minute guide

You can integrate SensorToolkit into your game in no time at all. Let's say you have an Enemy in your game, and you want the Enemy to detect when the Player is nearby and attack them. Here's what you can do:

  • Add a Range Sensor component to the Enemy. If your game is 2D then add the Range Sensor 2D component instead.
  • Configure the Range Sensor. Set its size and shape, and make sure Detects On Layers has the physics layer the Player is on. You probably want to set Detection Mode to Rigid Bodies as well.
  • Click the 'Test' button. This will Pulse the sensor in editor mode and show you everything it detects. Does the sensor detect the player? If the player wasn't in range then move it closer and click 'Test' again.

If the sensor can detect the Player then all good you can move on. If not then edit the settings and try again. If you continue to have issues make sure the Player has a Collider and a RigidBody component and that the correct physics layer is assigned on the sensor.

Integration with scripts

Let's say you're writing a C# script called EnemyBehaviour. This script will be attached to the Enemy and implement it's AI behaviour, if the player is in range then attack. The script would look something like this.

using UnityEngine;
using Micosmo.SensorToolkit;

public class EnemyBehaviour : MonoBehaviour {
Sensor sensor;

void Awake() {
sensor = GetComponent<Sensor>(); // RangeSensor extends the base class: Sensor
}

void Update() {
var player = sensor.GetNearestDetection("Player"); // Assuming the player has the 'Player' tag assigned
if (player != null) {
// Attack behaviour here...
}
}
}

Integration with Playmaker

If you're using Playmaker then you'll want to use the Sensor Get Detections action. Configure it so it looks like the image below. Each frame the action checks for the nearest detection with the 'Player' tag.

In a subsequent state you may want to check that the player is still detected by the sensor. Use the Sensor Get Signal action for that.

Next Steps

Using any of the sensors follows a similar process as above. Add the sensor you need, configure it, test it and integrate with your game logic.

Have a look at the Fundamentals.unity scene in Assets/SensorToolkit/Examples/, it has a sequence of hand-crafted demo-stations for each type of sensor. The other example scenes show how the sensors can be used to implement various mechanics.

Most of the time you'll be using the Ray Sensor, Range Sensor, Trigger Sensor or Line of Sight Sensor. These are the best to learn first.