using UnityEngine;
using System.Collections;
public class VisitorAI : MonoBehaviour {
NavMeshAgent agent;
public GameObject sleepRestorePrefab;
public float rest = 100f;
void Start () {
agent = GetComponent<NavMeshAgent> ();
}
void Update () {
if (rest > 1f )
{
rest -= Time.deltaTime * 5;
agent.SetDestination (Random.insideUnitSphere * 350); // Agent is currently wondering around as it is healthy
gameObject.GetComponent<Renderer>().material.color = Color.green; // Colour indicates that agent is completely healthy
}
if (rest < 5f )
{
gameObject.GetComponent<Renderer>().material.color = Color.black; // Changes colour of agent once they get tired
agent.SetDestination = sleepRestorePrefab.transform.position; // Tells the agent to go to instantiated building's position
{
// OnTriggerEnter information - AI Enters trigger radius to get sleep
void OnTriggerEnter(Collider other)
if(other.tag == "sleepRecoveryRadius") // Detects whether agent is close enough to the building to increase sleep
{
rest += Time.deltaTime * 10; // Increases sleep
Debug.Log ("Sleep is being restored");
}
}
else if ( rest > 99f )
{
gameObject.GetComponent<Renderer>().material.color = Color.green; // Agent is healthy again, so it goes back to original colour
agent.SetDestination (Random.insideUnitSphere * 350); // Tells the agent to wonder around again now it is healthy
}
}
}
}
My brains a bit frazzled so I’m sucking it up and asking for help as I need a fresh pair of eyes to look at where I’ve gone wrong. I’m nearly finished with this little bit of code and to be helpful as it’s messy right now I’ve added some comments so everybody knows what’s going on.
Right now I’m entirely focused on getting this trigger I have set up to work, it’s a very simple idea, when the variable rest in question goes down, then the agent with the script attached will immediately head off to whichever object is labelled to restore the rest variable. Only problem is I just haven’t got the syntax right, I realised when I was messing around with this stuff that I needed a trigger so that the agents wouldn’t just automatically recover the variable right away but Unity just doesn’t like the way I’ve put it in here.