Basic AI Bug i need help with
Hello ! basically, MY AI just stops after the first waypoint, Furtheron it slides around the map rather than using the prefabs animation
as shown here link: https://goo.gl/photos/tCPc1jeXdUNyrBbz8
the code i used is
//using System; using System.Collections; using UnityEngine.AI; using UnityEngine;
namespace UnityStandardAssets.Characters.ThirdPerson { public class AI : MonoBehaviour { public NavMeshAgent agent; public ThirdPersonCharacter Character; public enum State {PETROL, CHASE} public State state; // Checking the state of the AI. private bool Alive; // Cheacking Alive or not. // VAR FOR PETROLING public GameObject[] Waypoints; private int WayPointIndex=0; public float PETROL_SPEED = 10f; // Variables for chasing public float CHASE_SPEED = 20f; public GameObject target;
// Use this for initialization
void Start()
{
agent = GetComponent<NavMeshAgent>();
Character = GetComponent<ThirdPersonCharacter>();
agent.updatePosition = true;
agent.updateRotation = false;
state = AI.State.PETROL;
Alive = true;
StartCoroutine("FSM");
//Start FSM;
}
IEnumerator FSM()
{
while(Alive)
{
switch (state)
{
case State.PETROL:
Patrol();
break;
case State.CHASE:
Chase();
break;
}
}
yield return null;
}
void Patrol()
{
agent.speed = PETROL_SPEED;
if(Vector3.Distance(this.transform.position,Waypoints[WayPointIndex].transform.position)>=2f)
{
agent.SetDestination(Waypoints[WayPointIndex].transform.position);
Character.Move(agent.desiredVelocity, false, false);
Patrol();
}
else if(Vector3.Distance(this.transform.position, Waypoints[WayPointIndex].transform.position)<=2f)
{
WayPointIndex ++; // lol short hand
if(WayPointIndex >=Waypoints.Length)
{
WayPointIndex = 0;
}
}
else
{
Character.Move(Vector3.zero, false, false);
}
}
void Chase()
{
agent.speed = CHASE_SPEED;
agent.SetDestination(target.transform.position);
Character.Move(agent.desiredVelocity, false, false);
}
void OnTriggerEnter(Collider other)
{
if(other.tag=="Player")
{
state = AI.State.CHASE;
target = other.gameObject;
}
}
}
}
Your answer
Follow this Question
Related Questions
UnityEngine.UI.Image keeps removing itself from a Prefab. 1 Answer
Bizarre compile errors in Unity while Visual Studio will build project successfully 0 Answers
Can't select button in ScrollArea when moving from ScrollBar to button with mouse button pressed 1 Answer
Camera transform inexplicably locked, screen shake not working 0 Answers
Spawn Hazard, clone behave diffrent 0 Answers