1
\$\begingroup\$

I'm running into an issue where my click to move character has problems navigating when in an enemy's detection trigger collider. Specifically, whenever my character is inside the trigger collider, there seems to be "inertia" or refusal to navigate properly. Movement is ordinarily very responsive because of how I have the Nav Mesh Agent component set up. When I'm in the trigger, something seems to be wrong with the pathfinding, but I can still force myself to get out.

I have a very basic click to move player movement script.

    public class PlayerMovement : MonoBehaviour 
    {
        private NavMeshAgent navMeshAgent;

        void Start()
        {
            navMeshAgent = GetComponent<NavMeshAgent>();
        }

        void FixedUpdate()
        {
            if (Input.GetMouseButton(0))
            {
                Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                RaycastHit hit;

                if (Physics.Raycast(ray, out hit))
                {
                    navMeshAgent.destination = hit.point;
                }
            }
        }
    }

I also have a very standard enemy detection script attached to an empty game object which is a child of the enemy. I've done it this way so my enemy object itself doesn't have a bunch of colliders.

public class EnemyDetection : MonoBehaviour 
{
    private GameObject player;

    public bool playerDetected;

    void Start()
    {
        player = GameObject.FindGameObjectWithTag("Player");
    }

    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject == player)
        {
            playerDetected = true;
        }
    }

    void OnTriggerExit(Collider other)
    {
        if (other.gameObject == player)
        {
            playerDetected = false;
        }
    }
}

Finally, I have an enemy movement script attached to the enemy object which handles the actual nav mesh destination setting.

public class EnemyMovement : MonoBehaviour 
{
    private GameObject detectionTrigger;
    private EnemyDetection enemyDetection;

    private NavMeshAgent navMeshAgent;
    private GameObject player;

    void Start()
    {
        detectionTrigger = transform.Find("Detection Trigger").gameObject;
        enemyDetection = detectionTrigger.GetComponent<EnemyDetection>();

        navMeshAgent = GetComponent<NavMeshAgent>();
        player = GameObject.FindGameObjectWithTag("Player");
    }

    void Update()
    {
        if (enemyDetection.playerDetected)
        {
            navMeshAgent.SetDestination(player.transform.position);
        }
    }
}
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

I figured it out. The solution was to change the layer of the detection trigger object to "Ignore Raycast".

\$\endgroup\$
1
  • \$\begingroup\$ Please consider accepting this answer :) \$\endgroup\$
    – Vaillancourt
    Commented Mar 29, 2018 at 1:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .