RaycastHit on Object, instead of behind it

Hey everyone, I apologize if this has been asked, but I couldn’t find anything talking about - maybe because it’s a little hard to describe. I have a scene where I am in third person mode, and I am using mouse clicks to move a character. I’m using raycasts from the main camera to get the clicked location, and for the most part, this is working great. However, I’m running into an issue when I have an NPC in the scene. If you click on the NPC, the raycast is actually registering on the ground behind the NPC, causing the player to just walk right through them, instead of stopping in front of them like I want. I had this working in a script I wrote myself, but I replaced my script with one from Dialogue System for Unity, and for the life of me, I cannot figure out what is causing this to happen in the new script, but not the old.

Edit - I forgot to mention that I added a debug in to see if the raycast was actually hitting the NPC, and it definitely is.

Script is below, and thanks in advance for any kind of suggestions:

// Based on: http://wiki.unity3d.com/index.php/Click_To_Move_C
// By: Vinicius Rezendrix
using UnityEngine;
using System.Collections;

namespace PixelCrushers.DialogueSystem {
	
	/// <summary>
	/// Navigates to the place where the player mouse clicks.
	/// </summary>
	[AddComponentMenu("Dialogue System/Actor/Player/Navigate On Mouse Click")]
	[RequireComponent(typeof(NavMeshAgent))]
	public class NavigateOnMouseClick : MonoBehaviour {
		
		public AnimationClip idle;
		public AnimationClip run;
        public Animator animator;
        public float inspectDistance = 10;

        public enum MouseButtonType { Left, Right, Middle };
		public MouseButtonType mouseButton = MouseButtonType.Left;
		
		private Transform myTransform;
		private NavMeshAgent navMeshAgent;
        private Vector3 position;
        private Transform target;

        void Awake() {
			myTransform = transform;
            animator.GetComponent<Animator>();
			navMeshAgent = GetComponent<NavMeshAgent>();
            animator.SetBool("isWalking", false);
            if (navMeshAgent == null) {
				Debug.LogWarning(string.Format("{0}: No NavMeshAgent found on {1}. Disabling {2}.", DialogueDebug.Prefix, name, this.GetType().Name));
				enabled = false;
			}
        }
	 
		void Update() {

            myTransform = transform;
	 
			// Moves the Player if the Left Mouse Button was clicked:
			if (Input.GetMouseButtonDown((int) mouseButton) && GUIUtility.hotControl == 0) {
				Plane playerPlane = new Plane(Vector3.up, myTransform.position);
				Ray ray = UnityEngine.Camera.main.ScreenPointToRay(Input.mousePosition);
				float hitdist = 0.0f;
	 
				if (playerPlane.Raycast(ray, out hitdist)) {

                    RaycastHit hit;

                    if (Physics.Raycast(ray, out hit))
                    {
                        target = hit.transform;
                        Quaternion newRotation = Quaternion.LookRotation(position - myTransform.position);
                        newRotation.x = 0f;
                        newRotation.z = 0f;
                        transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, Time.deltaTime * 10);

                        Debug.Log(hit.transform.gameObject.tag);

                        navMeshAgent.SetDestination(ray.GetPoint(hitdist));
                        if (animator.GetBool("isWalking") == false)
                        {
                            animator.SetBool("isWalking", true);
                            animator.Play("HumanoidRun");
                        }
                    }
                        
                }
			}

            // Moves the player if the mouse button is held down:
            else if (Input.GetMouseButton((int) mouseButton) && GUIUtility.hotControl == 0) {
	 
				Plane playerPlane = new Plane(Vector3.up, myTransform.position);
				Ray ray = UnityEngine.Camera.main.ScreenPointToRay(Input.mousePosition);
				float hitdist = 0.0f;
	 
				if (playerPlane.Raycast(ray, out hitdist)) {

                    RaycastHit hit;

                    if (Physics.Raycast(ray, out hit))
                    {
                        position = ray.GetPoint(hitdist);
                        target = hit.transform;

                        Quaternion newRotation = Quaternion.LookRotation(position - myTransform.position);
                        newRotation.x = 0f;
                        newRotation.z = 0f;
                        transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, Time.deltaTime * 10);

                        if (animator.GetBool("isWalking") == false)
                        {
                            animator.SetBool("isWalking", true);
                            animator.Play("HumanoidRun");
                        }
                        navMeshAgent.SetDestination(ray.GetPoint(hitdist));
                    }
                }
			}
            //play idle animation after player reaches destination
            if (Vector3.Distance(position, transform.position) <= navMeshAgent.stoppingDistance + 2)
            {
                animator.SetBool("isWalking", false);
            }
        }
    }
}

I thought I figured this out by simply adding 5 units to the navMeshAgent.stoppingDistance property, but it was actually still happening (albeit looking much better this time around). I don’t want it to just look like it’s working correctly, I want it to actually work correctly. After a whole lot of trial and error, I found the solution was a major overhaul of my character controller script. Essentially, I added a couple of if statements to check if you clicked an NPC, and if so, set the navMeshAgent.stoppingDistance to 4. Also, instead of setting the navmeshagent’s destination to the raycast hit.point, I grabbed the object that was hit, and set the destination to that object instead. That has definitely done the trick. Here is my new Update() code:

void Update() {

            myTransform = transform;

            // Moves the Player if the Left Mouse Button was clicked:
            if (Input.GetMouseButtonDown((int)mouseButton) && GUIUtility.hotControl == 0)
            {
                Plane playerPlane = new Plane(Vector3.up, myTransform.position);
                Ray ray = UnityEngine.Camera.main.ScreenPointToRay(Input.mousePosition);

                RaycastHit hit;

                if (Physics.Raycast(ray, out hit))
                {

                    position = hit.point;
                    target = hit.transform;
                    Quaternion newRotation = Quaternion.LookRotation(position - myTransform.position);
                    newRotation.x = 0f;
                    newRotation.z = 0f;
                    transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, Time.deltaTime * 10);

                    //Debug.Log(hit.transform.gameObject.tag);

                    if (target.gameObject.CompareTag("Villager"))
                    {
                        GameObject npc = hit.transform.gameObject;
                        Debug.Log(npc.name);
                        navMeshAgent.stoppingDistance = 4f;
                        if (animator.GetBool("isWalking") == false)
                        {
                            animator.SetBool("isWalking", true);
                            animator.Play("HumanoidRun");
                        }
                        navMeshAgent.SetDestination(npc.transform.position);
                    }
                    else
                    {
                        navMeshAgent.stoppingDistance = 0.5f;

                        if (animator.GetBool("isWalking") == false)
                        {
                            animator.SetBool("isWalking", true);
                            animator.Play("HumanoidRun");
                        }
                        navMeshAgent.SetDestination(hit.point);
                    }
                }

            }

            // Moves the player if the mouse button is held down:
            else if (Input.GetMouseButton((int)mouseButton) && GUIUtility.hotControl == 0)
            {

                Plane playerPlane = new Plane(Vector3.up, myTransform.position);
                Ray ray = UnityEngine.Camera.main.ScreenPointToRay(Input.mousePosition);

                RaycastHit hit;

                if (Physics.Raycast(ray, out hit))
                {

                    position = hit.point;
                    target = hit.transform;
                    Quaternion newRotation = Quaternion.LookRotation(position - myTransform.position);
                    newRotation.x = 0f;
                    newRotation.z = 0f;
                    transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, Time.deltaTime * 10);

                    //Debug.Log(hit.transform.gameObject.tag);

                    if (target.gameObject.CompareTag("Villager"))
                    {
                        GameObject npc = hit.transform.gameObject;
                        Debug.Log(npc.name);
                        navMeshAgent.stoppingDistance = 4f;
                        if (animator.GetBool("isWalking") == false)
                        {
                            animator.SetBool("isWalking", true);
                            animator.Play("HumanoidRun");
                        }
                        navMeshAgent.SetDestination(npc.transform.position);
                    }
                    else
                    {
                        navMeshAgent.stoppingDistance = 0.5f;

                        if (animator.GetBool("isWalking") == false)
                        {
                            animator.SetBool("isWalking", true);
                            animator.Play("HumanoidRun");
                        }
                        navMeshAgent.SetDestination(hit.point);
                    }
                }
            }

            //play idle animation after player reaches destination
            if (Vector3.Distance(position, transform.position) <= navMeshAgent.stoppingDistance + 2)
            {
                animator.SetBool("isWalking", false);
            }

        }