How to do it...

We will develop the scripts for representing odor particles and agents that are able to smell:

  1. Create the particle's script and define its member variables for computing its lifespan:
using UnityEngine; 
using System.Collections; 
 
public class OdourParticle : MonoBehaviour 
{ 
    public float timespan; 
    private float timer; 
} 
  1. Implement the Start function for proper validations:
void Start() 
{ 
    if (timespan < 0f) 
        timespan = 0f; 
    timer = timespan; 
}
  1. Implement the timer and destroy the object after its life cycle ends:
void Update() 
{ 
    timer -= Time.deltaTime; 
    if (timer < 0f) 
        Destroy(gameObject); 
} 
  1. Create the class for representing the sniffer agent:
using UnityEngine; using System.Collections; using System.Collections.Generic; ...

Get Unity 2018 Artificial Intelligence Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.