AI problem.

using UnityEngine;
using System.Collections;

public class personAI : MonoBehaviour {
	//current movement target
	public GameObject mTarget;
	//target variables
	public GameObject foodTarget;
	public GameObject bedTarget;
	//statusvariables
	public float hunger = 15;
	public float fatigue = 20;

	public bool busy = false;
	//init components


	//movement function
	public void moveWithin(GameObject target, int stopDistance, int speed){
		transform.LookAt (target.transform);
		//set the speed according to deltatime
		float step = Time.deltaTime * speed;
		//if its not too close to its target, move.
		float x = transform.position.x - target.transform.position.x;
		float y = transform.position.y - target.transform.position.y;
		if (Mathf.Sqrt ((x*x) + (y*y)) > stopDistance) {
			//do movement code
			transform.position = Vector3.MoveTowards (transform.position, target.transform.position, step);
		}	
	}
	public void eat(){
		//loop until hunger rises over 95
		while (hunger < 95) {
						mTarget = GameObject.Find ("food");
						moveWithin(mTarget, 5, 10);
						//handling the actual proximity refill of the person
						float x = transform.position.x - mTarget.transform.position.x;
						float y = transform.position.y - mTarget.transform.position.y;
								if (Mathf.Sqrt ((x * x) + (y * y)) <= 10) {
										hunger = hunger + (1 * Time.deltaTime);
								}
				}
		busy = false;
	}

	public void sleep(){
		//loop until fatigue rises over 95
		while (fatigue < 95) {
						mTarget = GameObject.Find ("bed");
						moveWithin(mTarget, 5, 10);
						//handling the actual proximity refill of the person {
						float x = transform.position.x - mTarget.transform.position.x;
						float y = transform.position.y - mTarget.transform.position.y;
								if (Mathf.Sqrt ((x * x) + (y * y)) <= 10) {
										fatigue = fatigue + (1 * Time.deltaTime);
								}
				}
		busy = false;
	}

	void Start () {

	}
	
	// Update is called once per frame
	void Update () {
		if (!busy) {
			if (hunger<10){
				busy = true;
				eat ();
			}
			if (fatigue<10){
				busy = true;
				sleep();
			}
		}

		//status degradation over time
		//currently removes 1 per second
		if (hunger > 0) {
			hunger = hunger -1 * Time.deltaTime;
		}
		if (fatigue > 0) {
			fatigue = fatigue - 1 * Time.deltaTime;
		}
			
	}
}

I’m trying to make a very basic AI for now, there is a countdown on hunger and fatigue. I want to set the busy variable to prevent another task starting and then run either the sleep() function or eat() function. EDIT: updated the code, it seems to complete the sleep() and eat() functions instantly. Can an experienced eye point it point it out to me please.

Any help would be grand, thanks.

Halbera.

I’m not that experienced, but are you trying to implement an IEnumerator? Instead of running things instantly you can put one in and it won’t execute your method right away.

No, I don’t want to delay it from starting, I wanted it to run through the function as if it were in the update function. Ive seen the problem and I’ve tried a different approach but it’s not working either.

This seems to work but it isn’t checking distances correctly, it will start adding hunger from its current location, it doesn’t wait to get in range.

using UnityEngine;
using System.Collections;

public class personAI : MonoBehaviour {
	//current movement target
	public GameObject mTarget;
	//target variables
	public GameObject foodTarget;
	public GameObject bedTarget;
	//statusvariables
	public float hunger = 15;
	public float fatigue = 20;
	
	int currentAction;
	
	public bool busy = false;
	//init components
	
	
	//movement function
	public void moveWithin(GameObject target, int stopDistance, int speed){
		transform.LookAt (target.transform);
		//set the speed according to deltatime
		float step = Time.deltaTime * speed;
		//if its not too close to its target, move.
		float x = transform.position.x - target.transform.position.x;
		float y = transform.position.y - target.transform.position.y;
		if (Mathf.Sqrt ((x*x) + (y*y)) > stopDistance) {
			//do movement code
			transform.position = Vector3.MoveTowards (transform.position, target.transform.position, step);
		}	
	}
	public void doAction(int action){
		switch (action){
			//loop until hunger rises over 95
		case 0:	
			if (hunger < 95) {
				mTarget = GameObject.Find ("food");
				moveWithin (mTarget, 5, 10);
				//handling the actual proximity refill of the person
				float x = transform.position.x - mTarget.transform.position.x;
				float y = transform.position.y - mTarget.transform.position.y;
				if (Mathf.Sqrt ((x * x) + (y * y)) <= 10) {
					hunger = hunger + (10 * Time.deltaTime);
				}
			}
			if (hunger >= 95){
				busy = false;
			}
			break;
			
			
		case 1:
			if (fatigue < 95) {
				mTarget = GameObject.Find ("bed");
				moveWithin (mTarget, 5, 10);
				//handling the actual proximity refill of the person {
				float x = transform.position.x - mTarget.transform.position.x;
				float y = transform.position.y - mTarget.transform.position.y;
				if (Mathf.Sqrt ((x * x) + (y * y)) <= 10) {
					fatigue = fatigue + (10 * Time.deltaTime);
				}
			}
			if (fatigue >= 95){
				busy = false;
			}
			break;
		}
	}
	
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		if (!busy) {
			if (hunger < 10){
				currentAction = 0;
				busy = true;
			}
			if (fatigue < 10){
				currentAction = 1;
				busy = true;
			}
			
		}
		if (busy) {
			doAction (currentAction);
		}
		
		//status degradation over time
		//currently removes 1 per second
		if (hunger > 0) {
			hunger = hunger -1 * Time.deltaTime;
		}
		if (fatigue > 0) {
			fatigue = fatigue - 1 * Time.deltaTime;
		}
		
	}
}

EDIT:

using UnityEngine;
using System.Collections;

public class personAI : MonoBehaviour {
	//current movement target
	public GameObject mTarget;
	//target variables
	public GameObject foodTarget;
	public GameObject bedTarget;
	//statusvariables
	public float hunger = 15;
	public float fatigue = 20;
	
	int currentAction;
	
	public bool busy = false;
	//init components
	
	
	//movement function
	public void moveWithin(GameObject target, int stopDistance, int speed){
		transform.LookAt (target.transform);
		//set the speed according to deltatime
		float step = Time.deltaTime * speed;
		//if its not too close to its target, move.
		float x = transform.position.x - target.transform.position.x;
		float y = transform.position.y - target.transform.position.y;
		if (Mathf.Sqrt ((x*x) + (y*y)) > stopDistance) {
			//do movement code
			transform.position = Vector3.MoveTowards (transform.position, target.transform.position, step);
		}	
	}
	public void doAction(){
		switch (currentAction){
			//loop until hunger rises over 95
		case 1:	
			if (hunger < 95) {
				mTarget = GameObject.Find ("food");
				moveWithin (mTarget, 5, 10);
				//handling the actual proximity refill of the person
				float x = transform.position.x - mTarget.transform.position.x;
				float y = transform.position.y - mTarget.transform.position.y;
				if (Mathf.Sqrt ((x * x) + (y * y)) <= 10) {
					hunger = hunger + (10 * Time.deltaTime);
				}
			}
			if (hunger >= 95){
				busy = false;
				currentAction = 0;
			}
			break;
			
			
		case 2:
			if (fatigue < 95) {
				mTarget = GameObject.Find ("bed");
				moveWithin (mTarget, 5, 10);
				//handling the actual proximity refill of the person {
				float x = transform.position.x - mTarget.transform.position.x;
				float y = transform.position.y - mTarget.transform.position.y;
				if (Mathf.Sqrt ((x * x) + (y * y)) <= 10) {
					fatigue = fatigue + (10 * Time.deltaTime);
				}
			}
			if (fatigue >= 95){
				busy = false;
				currentAction = 0;
			}
			break;
		}
	}
	
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		if (!busy) {
			if (hunger < 10){
				currentAction = 1;
				busy = true;
			}
			if (fatigue < 10){
				currentAction = 2;
				busy = true;
			}
			
		}
		if (busy) {
			doAction ();
		}
		
		//status degradation over time
		//currently removes 1 per second
		if (hunger > 0) {
			hunger = hunger -1 * Time.deltaTime;
		}
		if (fatigue > 0) {
			fatigue = fatigue - 1 * Time.deltaTime;
		}
		
	}
}

This works, but the very first time it runs through it doesnt move but still thinks its in range and fills up, after the first run through its fine, any ideas?