I am creating an AI that I want to wander around and when it detects food in within a radius it goes towards it. Here is my code so far, currently it just wanders around. How do I impliment this to get it to find food and go towards it?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
using System.Linq;
public class Movement : MonoBehaviour
{
public float speed = 1f;
public float rotSpeed = 100f;
NavMeshAgent agent;
private bool isWandering = false;
private bool rotatingLeft = false;
private bool rotatingRight = false;
private bool isWalking = false;
void Start()
{
agent = this.GetComponent<NavMeshAgent>();
}
void LateUpdate()
{
StartCoroutine(WanderParent());
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("PickUp"))
{
other.gameObject.SetActive(false);
}
}
IEnumerator WanderParent()
{
if (isWandering == false)
{
StartCoroutine(Wander());
}
if (rotatingRight == true)
{
transform.Rotate(transform.up * Time.deltaTime * rotSpeed);
}
if (rotatingLeft == true)
{
transform.Rotate(transform.up * Time.deltaTime * -rotSpeed);
}
if (isWalking == true)
{
transform.position += transform.forward * speed * Time.deltaTime;
}
yield return new WaitForSeconds(0);
}
IEnumerator Wander()
{
int rotTime = Random.Range(1, 10);
int rotWait = Random.Range(1, 4);
int rotLR = Random.Range(0, 3);
isWandering = false;
isWalking = true;
yield return new WaitForSeconds(0);
if(rotLR == 1)
{
rotatingRight = true;
yield return new WaitForSeconds(rotTime);
rotatingRight = false;
}
if (rotLR == 2)
{
rotatingLeft = true;
yield return new WaitForSeconds(rotTime);
rotatingLeft = false;
}
isWandering = false;
}
}
,Hello, I am creating an AI that wanders around and when it detects food within its radius it goes towards it. Here is what I have so far:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
using System.Linq;
public class Movement : MonoBehaviour
{
public float speed = 1f;
public float rotSpeed = 100f;
NavMeshAgent agent;
private bool isWandering = false;
private bool rotatingLeft = false;
private bool rotatingRight = false;
private bool isWalking = false;
void Start()
{
agent = this.GetComponent<NavMeshAgent>();
}
void LateUpdate()
{
StartCoroutine(WanderParent());
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("PickUp"))
{
other.gameObject.SetActive(false);
}
}
IEnumerator WanderParent()
{
if (isWandering == false)
{
StartCoroutine(Wander());
}
if (rotatingRight == true)
{
transform.Rotate(transform.up * Time.deltaTime * rotSpeed);
}
if (rotatingLeft == true)
{
transform.Rotate(transform.up * Time.deltaTime * -rotSpeed);
}
if (isWalking == true)
{
transform.position += transform.forward * speed * Time.deltaTime;
}
yield return new WaitForSeconds(0);
}
IEnumerator Wander()
{
int rotTime = Random.Range(1, 10);
int rotWait = Random.Range(1, 4);
int rotLR = Random.Range(0, 3);
isWandering = false;
isWalking = true;
yield return new WaitForSeconds(0);
if(rotLR == 1)
{
rotatingRight = true;
yield return new WaitForSeconds(rotTime);
rotatingRight = false;
}
if (rotLR == 2)
{
rotatingLeft = true;
yield return new WaitForSeconds(rotTime);
rotatingLeft = false;
}
isWandering = false;
}
}
What code do I have to add to get it to detect a game object within a radius and go towards it?