Sensors

Using the Mecanim state machine in this way is very powerful and just having scripts update the parameters of the state machine through input (user taps a key, or scene loads) is simple enough. However, if you want reactive AI, you might want to think about sensors.

Sensors are effectively the AI's eyes and ears and whatever else it wants to use to detect action within a scene (even if it's an alarm or trip wire). Generally, they are self-contained components that look after themselves and inform whatever they are attached to. They can be as complex or as simple as you need them to be.

A basic sensor might be an empty game object with a trigger collider (the trip wire), which tells the enemy state machine that the player has come into view. Alternatively, you could use ray casting (yes, even in 2D) to check whether the target is in view.

One of the best examples of a sensor I've seen is a wandering game object with a sphere trigger that wanders round the screen to represent the point where the enemy was looking at. If it falls on the player or an object that has been moved in the scene, then all hell breaks loose.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.52.188