The question is answered, right answer was accepted
How to stop enemy following the player
Hi guys! Can someone help me please with my script? How to stop enemy when player is on some distance from him? And how to make him going when player is near enemy? Here is the code and now enemy stop only when the player is dead. But if the player is alive - he is following the player all the time. How to change it?
Thx for attention!
using UnityEngine;
using System.Collections;
public class EnemyMovement : MonoBehaviour
{
Transform player;
PlayerHealth playerHealth;
EnemyHealth enemyHealth;
UnityEngine.AI.NavMeshAgent nav;
void Awake ()
{
player = GameObject.FindGameObjectWithTag ("Player").transform;
playerHealth = player.GetComponent <PlayerHealth> ();
enemyHealth = GetComponent <EnemyHealth> ();
nav = GetComponent <UnityEngine.AI.NavMeshAgent> ();
}
void Update ()
{
if(enemyHealth.currentHealth > 0 && playerHealth.currentHealth > 0)
{
nav.SetDestination (player.position);
}
else
{
nav.enabled = false;
}
}
}
Answer by Onk3y · Aug 14, 2017 at 10:46 AM
Instead of using
nav.enabled = false
try to use
nav.ResetPath()
This will clear the Destination of your NavMeshAgent. If you want your agent to follow the Player again just call NavMeshAgent.SetDestination(player.position) again. nav.enabled is used for (de-)activating the component and that is not what you want to do. Hope this works for you.
thx for reply, but it`s not working =(((
maybe i have wrote something wrong?
using UnityEngine;
using System.Collections;
public class Enemy$$anonymous$$ovement : $$anonymous$$onoBehaviour
{
Transform player;
PlayerHealth playerHealth;
EnemyHealth enemyHealth;
UnityEngine.AI.Nav$$anonymous$$eshAgent nav;
void Awake ()
{
player = GameObject.FindGameObjectWithTag ("Player").transform;
playerHealth = player.GetComponent <PlayerHealth> ();
enemyHealth = GetComponent <EnemyHealth> ();
nav = GetComponent <UnityEngine.AI.Nav$$anonymous$$eshAgent> ();
}
void Update ()
{
if(enemyHealth.currentHealth > 0 && playerHealth.currentHealth > 0)
{
nav.SetDestination (player.position);
}
else
{
nav.ResetPath();
nav.SetDestination (player.position);
}
}
}
@thenoodlegrush Actually there is a small mistake in your code. The line of code after "nav.ResetPath();" will screw everything up because you immediately set a new destination after you've deleted your old one. Just delete the line "nav.SetDestination (player.position);" after the line "nav.ResetPath()" and you're good to go (delete line 30). It might have been a bit unclear in my original answer but call "nav.SetDestination (player.position);" to follow the player and call "nav.ResetPath()" if you want to stop following the player.
i have deleted this line, but enemy still chasing the player, even if the player is on really big distance - he still following the player. how to setup correctly the distance after which he will be stopping? and when player will back on this distance - he will following player again?
Answer by Tomikip · Aug 03, 2020 at 11:25 AM
You can try this if (Vector2.Distance(transform.position, target.position) > stoppingDistance) { transform.position = Vector2.MoveTowards(transform.position, target.position, MoveSpeed Time.deltaTime); } else if (Vector2.Distance(transform.position, target.position) < stoppingDistance && (Vector2.Distance(transform.position, target.position) > retreatDistance)) { transform.position = this.transform.position; } else if ((Vector2.Distance(transform.position, target.position) < retreatDistance)) { transform.position = Vector2.MoveTowards(transform.position, target.position, -MoveSpeed Time.deltaTime); }
In the update method (my game is in 2d so you will probably need Vector3 i dont really know ^^)