- Home /
Question by
lightyearz2 · Jan 27, 2018 at 02:25 AM ·
c#aipatrolno error
AI patrol not working
I want the AI to patrol if it hasn't seen the player. It just stays there. Here is my code :`using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.AI;
public class SpitterAI : MonoBehaviour {
public NavMeshAgent nav;
public Transform target;
public int fov = 110;
bool PlayerInSight = false;
SphereCollider col;
public string state = "idle";
// Use this for initialization
void Start () {
nav = gameObject.GetComponent<NavMeshAgent>();
col = gameObject.GetComponent<SphereCollider>();
state = "idle";
}
// Update is called once per frame
void Update () {
if (PlayerInSight)
{
nav.SetDestination(target.transform.position);
}
if (state == "idle")
{
nav.SetDestination(transform.position += new Vector3(Random.Range(-10f, 10f), Random.Range(-10f, 10f), Random.Range(-10f, 10f)));
state = "running";
}
if (state == "running")
{
//play running animation
if (nav.isStopped)
{
state = "idle";
}
}
}
private void OnTriggerStay(Collider other)
{
if (other.gameObject.CompareTag("player"))
{
print("playerinter");
PlayerInSight = false;
float angle = Vector3.Angle(transform.position, transform.forward);
if (angle < fov * 0.5)
{
print("playerinfov");
PlayerInSight = true;
state = "running";
}
}
}
}
`
Comment
Your answer
Follow this Question
Related Questions
If one point is taken go the other point. Quick fix? 2 Answers
cannot make a list with the word hello in it 1 Answer
Distribute terrain in zones 3 Answers
oncollisionenter2d always collides 2 Answers
Monster AI patrol an idle 0 Answers