- Home /
Navmeshagent wont go after player after stopping!
Hey! i am makeing a game were a enemy bandit will follow me when i enter its radius. then if it gets to close it will stop and it does. But when i exit the stop range it will run away until it goes outside the Range then stops, and when i enter the range is runs until im outside its range? this is my script:
public class Enemy_Move : MonoBehaviour {
private Animator anim;
private NavMeshAgent agent;
public GameObject player;
public float Range;
public float attackRange;
public bool toClose = false;
private float distance;
void Start ()
{
player = GameObject.FindGameObjectWithTag("Player");
agent = GetComponent<NavMeshAgent>();
anim = GetComponent<Animator>();
}
void Update ()
{
distance = Vector3.Distance(player.transform.position, this.transform.position);
//Enemy detects player
if (distance <=Range && toClose == false)
{
agent.destination = player.transform.position;
//Animations
anim.SetBool("Walk", false);
anim.SetBool("Sprint", true);
anim.SetBool("Interact", false);
}
//Stops animation if get far away
if(distance >= Range)
{
anim.SetBool("Walk", false);
anim.SetBool("Sprint", false);
anim.SetBool("Interact", false);
}
//Stops if to close and when attacks!
if(distance < attackRange)
{
agent.isStopped = true;
toClose = true;
anim.SetBool("Walk", false);
anim.SetBool("Sprint", false);
anim.SetBool("Interact", false);
}
//Sets to close to false when exit attack range
if(distance > attackRange)
{
toClose = false;
}
}
private void OnDrawGizmos()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(transform.position, Range);
Gizmos.color = Color.blue;
Gizmos.DrawWireSphere(transform.position, attackRange);
Answer by Captain_Pineapple · Dec 02, 2018 at 11:51 AM
Hey there,
can't really see an obvious problem/solution to your issue but perhaps it might help if you rewrite your code like this:
if(distance < attackRange)
{
agent.isStopped = true;
anim.SetBool("Walk", false);
anim.SetBool("Sprint", false);
anim.SetBool("Interact", false);
}
else if (distance <=Range)
{
agent.destination = player.transform.position;
//Animations
anim.SetBool("Walk", false);
anim.SetBool("Sprint", true);
anim.SetBool("Interact", false);
}
//Stops animation if get far away
else if(distance >= Range)
{
anim.SetBool("Walk", false);
anim.SetBool("Sprint", false);
anim.SetBool("Interact", false);
}
this way you can make sure that you have defined "states" and that you will net set or reset any bools in one frame. Should make things easier to read and to debug. Also you probably want to change your animation states to something else than bool. In the end you might escape from your enemys "detection" range but it will keep moving since you dont reset the current destination point. But you will set the animation to false. This way your enemy will be moving in space but will not play any animation anymore.
Answer by Nocktion · Dec 02, 2018 at 01:48 PM
I think it would work much better if you'd use a state machine and it's also much simpler.
Your answer
Follow this Question
Related Questions
Nav Agent Moving Only Forwards And Backwards 0 Answers
All my NavMeshAgents are tilted 1 Answer
Complex Enemy follow AI 1 Answer
NavMeshAgent Finding Alternative Route 1 Answer
Stop enemy from moving with GetKeyDown 3 Answers