How can I get my NavMeshAgent to stop about 1.5 units from the selected target? I am trying to create a 3rd person camera, and I am trying to get these actions to work with NavMeshAgent for easier controlling for users (game requires little to no accuracy). Currently, the only thing that can be interacted with is a chunk of “gems” with the “Gem” script on them. I have my player stop “1.5f” units from the “Gem”. But then the part after it checks distance from “Gem” to make sure it is close enough to activate the collection of it (to stop it from activating it as soon as it is clicked, so that other players/NPC may have a chance at it too). Currently, it stops about 1.5 units it looks like, but it is never 1.5 FROM the direction the player is coming from (so it may over shoot it and walk over it). But even when that happens, the check for distance does not go off, leaving the gem uncollected for several dozen clicks (even through the distance appears to be less than 2 units).
using UnityEngine;
using System.Collections;
public class MouseControlledPlayer : MonoBehaviour {
public NavMeshAgent body;
public Camera cam;
void Start () {
body = gameObject.GetComponent<NavMeshAgent>();
}
void Update () {
RaycastHit hit;
if (Input.GetMouseButtonDown(0))
{
Ray ray = cam.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
if (hit.transform.GetComponent<Gem>())
{
print("Found Gem!");
body.SetDestination(hit.point - (transform.forward * 1.5f));
if (Vector3.Distance(hit.point, transform.position) <= 2)
{
hit.transform.GetComponent<Gem>().taken = true;
}
}
else
{
body.SetDestination(hit.point);
}
}
}
}
}