- Home /
I've a problem with my wandering AI
Hello I have a question. I made a wandering script in which I have a 'rabbit' that can walk around. Now I just want if he gets close to an apple (360 ° range) that he goes to the apple and eat it and then continue walking again. )
Can anyone help me? This is my script:
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Rabbit : MonoBehaviour { private UnityEngine.AI.NavMeshAgent agent;
public Transform rabbit;
private bool isWander;
[Header("Found something?")]
public bool foundFood;
public bool foundRabbit;
private float delay;
[Header("Stats")]
public float food;
public float health;
private void Awake()
{
isWander = true;
agent = GetComponent<UnityEngine.AI.NavMeshAgent>();
}
private void Update()
{
food -= 1 * Time.deltaTime;
if(isWander == true)
{
delay += 1 * Time.deltaTime;
agent.SetDestination(rabbit.transform.position);
if(delay >= 1)
{
transform.Rotate(0, Random.Range(10f, 360f), 0);
delay = 0;
}
}
}
private void OnTriggerEnter(Collider other)
{
if(other.tag == "food")
{
if (food < 30)
{
foundFood = true;
StartCoroutine(Eating(other));
isWander = false;
agent.SetDestination(other.transform.position);
}
}
}
private void OnTriggerExit(Collider other)
{
if(other.tag == "food")
{
foundFood = false;
isWander = true;
agent.SetDestination(rabbit.transform.position);
}
}
IEnumerator Eating(Collider other)
{
food =+ 30;
Destroy(other.gameObject);
agent.SetDestination(rabbit.transform.position);
yield return new WaitForSeconds(1);
}
}
Comment
Do you want us to write the script for you? What have you tried so far?
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Collider Vision AI question. Solved! 0 Answers
Problems when i have multiple enemies on the same scene C# 1 Answer