- Home /
Navmesh follow and stop at a distance
Hi so what im trying to create is. the play can right click on an enemy and he will follow at a certain distance. which is working fine. but what i want it to also do is stop at that distance too. currently if the enemy stops he will try and go to its exact position instead of stopping a little bit away this is what i have currently
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.AI;
 public class Attacking : MonoBehaviour {
     NavMeshAgent agent;
     Transform target;
     public float distance;
     public float followDistance;
     // Use this for initialization
     void Start () {
         agent = GetComponent<NavMeshAgent>();
     }
     
     // Update is called once per frame
     void Update () {
         if (Input.GetMouseButton(0))
         {
             target = null;
         }
             if (Input.GetMouseButton(1))
         {
             RaycastHit hit;
 
             if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 100))
             {
                 if (hit.collider.gameObject.tag == "enemy" || hit.collider.gameObject.tag == "Player")
                 {
                     target = hit.collider.transform;
                 }
                     
             }
         }
         if(target != null)
         {
             distance = Vector3.Distance(transform.position, target.position);
             if (followDistance <= distance)
                 agent.destination = target.position;
         }
     }
 }
 
               Comment
              
 
               
              Answer by GameDev_Chuck · May 25, 2017 at 09:03 PM
You should be able to set the stop distance in the NavMeshAgent Component in the inspector.
Your answer
 
 
             Follow this Question
Related Questions
Enemy following Player on uneven surface 1 Answer
Moving enemy toward player. 0 Answers
I changed my monitor resolution and now my enemies are chasing me too fast ?? 2 Answers
NavMesh agent bug 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                
